ipxfrm.c revision 8dbe67d2fe40fc4dc873610f8f20a5fa042cc4bc
1/* $USAGI: $ */
2
3/*
4 * Copyright (C)2004 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20/*
21 * based on ip.c, iproute.c
22 */
23/*
24 * Authors:
25 *	Masahide NAKAMURA @USAGI
26 */
27
28#include <alloca.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <time.h>
35#include <netdb.h>
36#include <linux/netlink.h>
37#include <linux/rtnetlink.h>
38#include <linux/xfrm.h>
39
40#include "utils.h"
41#include "xfrm.h"
42#include "ip_common.h"
43
44#define STRBUF_SIZE	(128)
45#define STRBUF_CAT(buf, str) \
46	do { \
47		int rest = sizeof(buf) - 1 - strlen(buf); \
48		if (rest > 0) { \
49			int len = strlen(str); \
50			if (len > rest) \
51				len = rest; \
52			strncat(buf, str, len); \
53			buf[sizeof(buf) - 1] = '\0'; \
54		} \
55	} while(0);
56
57struct xfrm_filter filter;
58
59static void usage(void) __attribute__((noreturn));
60
61static void usage(void)
62{
63	fprintf(stderr,
64		"Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
65		"where  XFRM-OBJECT := state | policy | monitor\n");
66	exit(-1);
67}
68
69/* This is based on utils.c(inet_addr_match) */
70int xfrm_addr_match(xfrm_address_t *x1, xfrm_address_t *x2, int bits)
71{
72	__u32 *a1 = (__u32 *)x1;
73	__u32 *a2 = (__u32 *)x2;
74	int words = bits >> 0x05;
75
76	bits &= 0x1f;
77
78	if (words)
79		if (memcmp(a1, a2, words << 2))
80			return -1;
81
82	if (bits) {
83		__u32 w1, w2;
84		__u32 mask;
85
86		w1 = a1[words];
87		w2 = a2[words];
88
89		mask = htonl((0xffffffff) << (0x20 - bits));
90
91		if ((w1 ^ w2) & mask)
92			return 1;
93	}
94
95	return 0;
96}
97
98int xfrm_xfrmproto_is_ipsec(__u8 proto)
99{
100	return (proto ==  IPPROTO_ESP ||
101		proto ==  IPPROTO_AH  ||
102		proto ==  IPPROTO_COMP);
103}
104
105int xfrm_xfrmproto_is_ro(__u8 proto)
106{
107	return (proto ==  IPPROTO_ROUTING ||
108		proto ==  IPPROTO_DSTOPTS);
109}
110
111struct typeent {
112	const char *t_name;
113	int t_type;
114};
115
116static const struct typeent xfrmproto_types[]= {
117	{ "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP },
118	{ "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS },
119	{ "ipsec-any", IPSEC_PROTO_ANY },
120	{ NULL, -1 }
121};
122
123int xfrm_xfrmproto_getbyname(char *name)
124{
125	int i;
126
127	for (i = 0; ; i++) {
128		const struct typeent *t = &xfrmproto_types[i];
129		if (!t->t_name || t->t_type == -1)
130			break;
131
132		if (strcmp(t->t_name, name) == 0)
133			return t->t_type;
134	}
135
136	return -1;
137}
138
139const char *strxf_xfrmproto(__u8 proto)
140{
141	static char str[16];
142	int i;
143
144	for (i = 0; ; i++) {
145		const struct typeent *t = &xfrmproto_types[i];
146		if (!t->t_name || t->t_type == -1)
147			break;
148
149		if (t->t_type == proto)
150			return t->t_name;
151	}
152
153	sprintf(str, "%u", proto);
154	return str;
155}
156
157static const struct typeent algo_types[]= {
158	{ "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH },
159	{ "comp", XFRMA_ALG_COMP }, { "aead", XFRMA_ALG_AEAD },
160	{ "auth-trunc", XFRMA_ALG_AUTH_TRUNC },
161	{ NULL, -1 }
162};
163
164int xfrm_algotype_getbyname(char *name)
165{
166	int i;
167
168	for (i = 0; ; i++) {
169		const struct typeent *t = &algo_types[i];
170		if (!t->t_name || t->t_type == -1)
171			break;
172
173		if (strcmp(t->t_name, name) == 0)
174			return t->t_type;
175	}
176
177	return -1;
178}
179
180const char *strxf_algotype(int type)
181{
182	static char str[32];
183	int i;
184
185	for (i = 0; ; i++) {
186		const struct typeent *t = &algo_types[i];
187		if (!t->t_name || t->t_type == -1)
188			break;
189
190		if (t->t_type == type)
191			return t->t_name;
192	}
193
194	sprintf(str, "%d", type);
195	return str;
196}
197
198const char *strxf_mask8(__u8 mask)
199{
200	static char str[16];
201	const int sn = sizeof(mask) * 8 - 1;
202	__u8 b;
203	int i = 0;
204
205	for (b = (1 << sn); b > 0; b >>= 1)
206		str[i++] = ((b & mask) ? '1' : '0');
207	str[i] = '\0';
208
209	return str;
210}
211
212const char *strxf_mask32(__u32 mask)
213{
214	static char str[16];
215
216	sprintf(str, "%.8x", mask);
217
218	return str;
219}
220
221const char *strxf_share(__u8 share)
222{
223	static char str[32];
224
225	switch (share) {
226	case XFRM_SHARE_ANY:
227		strcpy(str, "any");
228		break;
229	case XFRM_SHARE_SESSION:
230		strcpy(str, "session");
231		break;
232	case XFRM_SHARE_USER:
233		strcpy(str, "user");
234		break;
235	case XFRM_SHARE_UNIQUE:
236		strcpy(str, "unique");
237		break;
238	default:
239		sprintf(str, "%u", share);
240		break;
241	}
242
243	return str;
244}
245
246const char *strxf_proto(__u8 proto)
247{
248	static char buf[32];
249	struct protoent *pp;
250	const char *p;
251
252	pp = getprotobynumber(proto);
253	if (pp)
254		p = pp->p_name;
255	else {
256		sprintf(buf, "%u", proto);
257		p = buf;
258	}
259
260	return p;
261}
262
263const char *strxf_ptype(__u8 ptype)
264{
265	static char str[16];
266
267	switch (ptype) {
268	case XFRM_POLICY_TYPE_MAIN:
269		strcpy(str, "main");
270		break;
271	case XFRM_POLICY_TYPE_SUB:
272		strcpy(str, "sub");
273		break;
274	default:
275		sprintf(str, "%u", ptype);
276		break;
277	}
278
279	return str;
280}
281
282void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
283			__u8 mode, __u32 reqid, __u16 family, int force_spi,
284			FILE *fp, const char *prefix, const char *title)
285{
286	char abuf[256];
287
288	if (title)
289		fputs(title, fp);
290
291	memset(abuf, '\0', sizeof(abuf));
292	fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr),
293					   saddr, abuf, sizeof(abuf)));
294	memset(abuf, '\0', sizeof(abuf));
295	fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr),
296					  &id->daddr, abuf, sizeof(abuf)));
297	fprintf(fp, "%s", _SL_);
298
299	if (prefix)
300		fputs(prefix, fp);
301	fprintf(fp, "\t");
302
303	fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto));
304
305	if (show_stats > 0 || force_spi || id->spi) {
306		__u32 spi = ntohl(id->spi);
307		fprintf(fp, "spi 0x%08x", spi);
308		if (show_stats > 0)
309			fprintf(fp, "(%u)", spi);
310		fprintf(fp, " ");
311	}
312
313	fprintf(fp, "reqid %u", reqid);
314	if (show_stats > 0)
315		fprintf(fp, "(0x%08x)", reqid);
316	fprintf(fp, " ");
317
318	fprintf(fp, "mode ");
319	switch (mode) {
320	case XFRM_MODE_TRANSPORT:
321		fprintf(fp, "transport");
322		break;
323	case XFRM_MODE_TUNNEL:
324		fprintf(fp, "tunnel");
325		break;
326	case XFRM_MODE_ROUTEOPTIMIZATION:
327		fprintf(fp, "ro");
328		break;
329	case XFRM_MODE_IN_TRIGGER:
330		fprintf(fp, "in_trigger");
331		break;
332	case XFRM_MODE_BEET:
333		fprintf(fp, "beet");
334		break;
335	default:
336		fprintf(fp, "%u", mode);
337		break;
338	}
339	fprintf(fp, "%s", _SL_);
340}
341
342static const char *strxf_limit(__u64 limit)
343{
344	static char str[32];
345	if (limit == XFRM_INF)
346		strcpy(str, "(INF)");
347	else
348		sprintf(str, "%llu", (unsigned long long) limit);
349
350	return str;
351}
352
353void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
354{
355	if (prefix)
356		fputs(prefix, fp);
357	fprintf(fp, "stats:%s", _SL_);
358
359	if (prefix)
360		fputs(prefix, fp);
361	fprintf(fp, "  replay-window %u replay %u failed %u%s",
362		s->replay_window, s->replay, s->integrity_failed, _SL_);
363}
364
365static const char *strxf_time(__u64 time)
366{
367	static char str[32];
368
369	if (time == 0)
370		strcpy(str, "-");
371	else {
372		time_t t;
373		struct tm *tp;
374
375		/* XXX: treat time in the same manner of kernel's
376		 * net/xfrm/xfrm_{user,state}.c
377		 */
378		t = (long)time;
379		tp = localtime(&t);
380
381		strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
382	}
383
384	return str;
385}
386
387void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
388			 struct xfrm_lifetime_cur *cur,
389			 FILE *fp, const char *prefix)
390{
391	if (cfg) {
392		if (prefix)
393			fputs(prefix, fp);
394		fprintf(fp, "lifetime config:%s",_SL_);
395
396		if (prefix)
397			fputs(prefix, fp);
398		fprintf(fp, "  limit: soft %s(bytes),",
399			strxf_limit(cfg->soft_byte_limit));
400		fprintf(fp, " hard %s(bytes)%s",
401			strxf_limit(cfg->hard_byte_limit), _SL_);
402
403		if (prefix)
404			fputs(prefix, fp);
405		fprintf(fp, "  limit: soft %s(packets),",
406			strxf_limit(cfg->soft_packet_limit));
407		fprintf(fp, " hard %s(packets)%s",
408			strxf_limit(cfg->hard_packet_limit), _SL_);
409
410		if (prefix)
411			fputs(prefix, fp);
412		fprintf(fp, "  expire add: soft %llu(sec), hard %llu(sec)%s",
413			(unsigned long long) cfg->soft_add_expires_seconds,
414			(unsigned long long) cfg->hard_add_expires_seconds,
415			_SL_);
416
417		if (prefix)
418			fputs(prefix, fp);
419		fprintf(fp, "  expire use: soft %llu(sec), hard %llu(sec)%s",
420			(unsigned long long) cfg->soft_use_expires_seconds,
421			(unsigned long long) cfg->hard_use_expires_seconds,
422			_SL_);
423	}
424	if (cur) {
425		if (prefix)
426			fputs(prefix, fp);
427		fprintf(fp, "lifetime current:%s", _SL_);
428
429		if (prefix)
430			fputs(prefix, fp);
431		fprintf(fp, "  %llu(bytes), %llu(packets)%s",
432			(unsigned long long) cur->bytes,
433			(unsigned long long) cur->packets,
434			 _SL_);
435
436		if (prefix)
437			fputs(prefix, fp);
438		fprintf(fp, "  add %s ", strxf_time(cur->add_time));
439		fprintf(fp, "use %s%s", strxf_time(cur->use_time), _SL_);
440	}
441}
442
443void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
444			 FILE *fp, const char *prefix)
445{
446	char abuf[256];
447	__u16 f;
448
449	f = sel->family;
450	if (f == AF_UNSPEC)
451		f = family;
452	if (f == AF_UNSPEC)
453		f = preferred_family;
454
455	if (prefix)
456		fputs(prefix, fp);
457
458	memset(abuf, '\0', sizeof(abuf));
459	fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr),
460					      &sel->saddr, abuf, sizeof(abuf)),
461		sel->prefixlen_s);
462
463	memset(abuf, '\0', sizeof(abuf));
464	fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, sizeof(sel->daddr),
465					      &sel->daddr, abuf, sizeof(abuf)),
466		sel->prefixlen_d);
467
468	if (sel->proto)
469		fprintf(fp, "proto %s ", strxf_proto(sel->proto));
470	switch (sel->proto) {
471	case IPPROTO_TCP:
472	case IPPROTO_UDP:
473	case IPPROTO_SCTP:
474	case IPPROTO_DCCP:
475	default: /* XXX */
476		if (sel->sport_mask)
477			fprintf(fp, "sport %u ", ntohs(sel->sport));
478		if (sel->dport_mask)
479			fprintf(fp, "dport %u ", ntohs(sel->dport));
480		break;
481	case IPPROTO_ICMP:
482	case IPPROTO_ICMPV6:
483		/* type/code is stored at sport/dport in selector */
484		if (sel->sport_mask)
485			fprintf(fp, "type %u ", ntohs(sel->sport));
486		if (sel->dport_mask)
487			fprintf(fp, "code %u ", ntohs(sel->dport));
488		break;
489	case IPPROTO_GRE:
490		if (sel->sport_mask || sel->dport_mask)
491			fprintf(fp, "key %u ",
492				(((__u32)ntohs(sel->sport)) << 16) +
493				ntohs(sel->dport));
494		break;
495	case IPPROTO_MH:
496		if (sel->sport_mask)
497			fprintf(fp, "type %u ", ntohs(sel->sport));
498		if (sel->dport_mask) {
499			if (show_stats > 0)
500				fprintf(fp, "(dport) 0x%.4x ", sel->dport);
501		}
502		break;
503	}
504
505	if (sel->ifindex > 0)
506		fprintf(fp, "dev %s ", ll_index_to_name(sel->ifindex));
507
508	if (show_stats > 0)
509		fprintf(fp, "uid %u", sel->user);
510
511	fprintf(fp, "%s", _SL_);
512}
513
514static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
515			      FILE *fp, const char *prefix, int newline)
516{
517	int keylen;
518	int i;
519
520	if (prefix)
521		fputs(prefix, fp);
522
523	fprintf(fp, "%s ", strxf_algotype(type));
524
525	if (len < sizeof(*algo)) {
526		fprintf(fp, "(ERROR truncated)");
527		goto fin;
528	}
529	len -= sizeof(*algo);
530
531	fprintf(fp, "%s ", algo->alg_name);
532
533	keylen = algo->alg_key_len / 8;
534	if (len < keylen) {
535		fprintf(fp, "(ERROR truncated)");
536		goto fin;
537	}
538
539	if (keylen > 0) {
540		fprintf(fp, "0x");
541		for (i = 0; i < keylen; i ++)
542			fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
543
544		if (show_stats > 0)
545			fprintf(fp, " (%d bits)", algo->alg_key_len);
546	}
547
548 fin:
549	if (newline)
550		fprintf(fp, "%s", _SL_);
551}
552
553static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
554				   FILE *fp, const char *prefix)
555{
556	return __xfrm_algo_print(algo, type, len, fp, prefix, 1);
557}
558
559static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
560			    FILE *fp, const char *prefix)
561{
562	struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
563
564	memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
565	base_algo->alg_key_len = algo->alg_key_len;
566	memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
567
568	__xfrm_algo_print(base_algo, XFRMA_ALG_AEAD, len, fp, prefix, 0);
569
570	fprintf(fp, " %d", algo->alg_icv_len);
571
572	fprintf(fp, "%s", _SL_);
573}
574
575static void xfrm_auth_trunc_print(struct xfrm_algo_auth *algo, int len,
576				  FILE *fp, const char *prefix)
577{
578	struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
579
580	memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
581	base_algo->alg_key_len = algo->alg_key_len;
582	memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
583
584	__xfrm_algo_print(base_algo, XFRMA_ALG_AUTH_TRUNC, len, fp, prefix, 0);
585
586	fprintf(fp, " %d", algo->alg_trunc_len);
587
588	fprintf(fp, "%s", _SL_);
589}
590
591static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
592			    __u16 family, FILE *fp, const char *prefix)
593{
594	int ntmpls = len / sizeof(struct xfrm_user_tmpl);
595	int i;
596
597	if (ntmpls <= 0) {
598		if (prefix)
599			fputs(prefix, fp);
600		fprintf(fp, "(ERROR \"tmpl\" truncated)");
601		fprintf(fp, "%s", _SL_);
602		return;
603	}
604
605	for (i = 0; i < ntmpls; i++) {
606		struct xfrm_user_tmpl *tmpl = &tmpls[i];
607
608		if (prefix)
609			fputs(prefix, fp);
610
611		xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
612				   tmpl->reqid, tmpl->family, 0, fp, prefix, "tmpl ");
613
614		if (show_stats > 0 || tmpl->optional) {
615			if (prefix)
616				fputs(prefix, fp);
617			fprintf(fp, "\t");
618			switch (tmpl->optional) {
619			case 0:
620				if (show_stats > 0)
621					fprintf(fp, "level required ");
622				break;
623			case 1:
624				fprintf(fp, "level use ");
625				break;
626			default:
627				fprintf(fp, "level %u ", tmpl->optional);
628				break;
629			}
630
631			if (show_stats > 0)
632				fprintf(fp, "share %s ", strxf_share(tmpl->share));
633
634			fprintf(fp, "%s", _SL_);
635		}
636
637		if (show_stats > 0) {
638			if (prefix)
639				fputs(prefix, fp);
640			fprintf(fp, "\t");
641			fprintf(fp, "%s-mask %s ",
642				strxf_algotype(XFRMA_ALG_CRYPT),
643				strxf_mask32(tmpl->ealgos));
644			fprintf(fp, "%s-mask %s ",
645				strxf_algotype(XFRMA_ALG_AUTH),
646				strxf_mask32(tmpl->aalgos));
647			fprintf(fp, "%s-mask %s",
648				strxf_algotype(XFRMA_ALG_COMP),
649				strxf_mask32(tmpl->calgos));
650
651			fprintf(fp, "%s", _SL_);
652		}
653	}
654}
655
656int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp)
657{
658	int argc = *argcp;
659	char **argv = *argvp;
660
661	NEXT_ARG();
662	if (get_u32(&mark->v, *argv, 0)) {
663		invarg("Illegal \"mark\" value\n", *argv);
664	}
665	if (argc > 1)
666		NEXT_ARG();
667	else { /* last entry on parse line */
668		mark->m = 0xffffffff;
669		goto done;
670	}
671
672	if (strcmp(*argv, "mask") == 0) {
673		NEXT_ARG();
674		if (get_u32(&mark->m, *argv, 0)) {
675			invarg("Illegal \"mark\" mask\n", *argv);
676		}
677	} else {
678		mark->m = 0xffffffff;
679		PREV_ARG();
680	}
681
682done:
683	*argcp = argc;
684	*argvp = argv;
685
686	return 0;
687}
688
689void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
690		      FILE *fp, const char *prefix)
691{
692	if (tb[XFRMA_MARK]) {
693		struct rtattr *rta = tb[XFRMA_MARK];
694		struct xfrm_mark *m = (struct xfrm_mark *) RTA_DATA(rta);
695		fprintf(fp, "\tmark %d/0x%x\n", m->v, m->m);
696	}
697
698	if (tb[XFRMA_ALG_AUTH] && !tb[XFRMA_ALG_AUTH_TRUNC]) {
699		struct rtattr *rta = tb[XFRMA_ALG_AUTH];
700		xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
701				XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
702	}
703
704	if (tb[XFRMA_ALG_AUTH_TRUNC]) {
705		struct rtattr *rta = tb[XFRMA_ALG_AUTH_TRUNC];
706		xfrm_auth_trunc_print((struct xfrm_algo_auth *) RTA_DATA(rta),
707				      RTA_PAYLOAD(rta), fp, prefix);
708	}
709
710	if (tb[XFRMA_ALG_AEAD]) {
711		struct rtattr *rta = tb[XFRMA_ALG_AEAD];
712		xfrm_aead_print((struct xfrm_algo_aead *)RTA_DATA(rta),
713				RTA_PAYLOAD(rta), fp, prefix);
714	}
715
716	if (tb[XFRMA_ALG_CRYPT]) {
717		struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
718		xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
719				XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix);
720	}
721
722	if (tb[XFRMA_ALG_COMP]) {
723		struct rtattr *rta = tb[XFRMA_ALG_COMP];
724		xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
725				XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix);
726	}
727
728	if (tb[XFRMA_ENCAP]) {
729		struct xfrm_encap_tmpl *e;
730		char abuf[256];
731
732		if (prefix)
733			fputs(prefix, fp);
734		fprintf(fp, "encap ");
735
736		if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
737			fprintf(fp, "(ERROR truncated)");
738			fprintf(fp, "%s", _SL_);
739			return;
740		}
741		e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
742
743		fprintf(fp, "type ");
744		switch (e->encap_type) {
745		case 1:
746			fprintf(fp, "espinudp-nonike ");
747			break;
748		case 2:
749			fprintf(fp, "espinudp ");
750			break;
751		default:
752			fprintf(fp, "%u ", e->encap_type);
753			break;
754		}
755		fprintf(fp, "sport %u ", ntohs(e->encap_sport));
756		fprintf(fp, "dport %u ", ntohs(e->encap_dport));
757
758		memset(abuf, '\0', sizeof(abuf));
759		fprintf(fp, "addr %s",
760			rt_addr_n2a(family, sizeof(e->encap_oa),
761				    &e->encap_oa, abuf, sizeof(abuf)));
762		fprintf(fp, "%s", _SL_);
763	}
764
765	if (tb[XFRMA_TMPL]) {
766		struct rtattr *rta = tb[XFRMA_TMPL];
767		xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta),
768				RTA_PAYLOAD(rta), family, fp, prefix);
769	}
770
771	if (tb[XFRMA_COADDR]) {
772		char abuf[256];
773		xfrm_address_t *coa;
774
775		if (prefix)
776			fputs(prefix, fp);
777		fprintf(fp, "coa ");
778
779		coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]);
780
781		if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
782			fprintf(fp, "(ERROR truncated)");
783			fprintf(fp, "%s", _SL_);
784			return;
785		}
786
787		memset(abuf, '\0', sizeof(abuf));
788		fprintf(fp, "%s",
789			rt_addr_n2a(family, sizeof(*coa), coa,
790				    abuf, sizeof(abuf)));
791		fprintf(fp, "%s", _SL_);
792	}
793
794	if (tb[XFRMA_LASTUSED]) {
795		__u64 lastused;
796
797		if (prefix)
798			fputs(prefix, fp);
799		fprintf(fp, "lastused ");
800
801		if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
802			fprintf(fp, "(ERROR truncated)");
803			fprintf(fp, "%s", _SL_);
804			return;
805		}
806
807		lastused = rta_getattr_u64(tb[XFRMA_LASTUSED]);
808
809		fprintf(fp, "%s", strxf_time(lastused));
810		fprintf(fp, "%s", _SL_);
811	}
812
813}
814
815static int xfrm_selector_iszero(struct xfrm_selector *s)
816{
817	struct xfrm_selector s0;
818
819	memset(&s0, 0, sizeof(s0));
820
821	return (memcmp(&s0, s, sizeof(s0)) == 0);
822}
823
824void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
825			    struct rtattr *tb[], FILE *fp, const char *prefix,
826			    const char *title)
827{
828	char buf[STRBUF_SIZE];
829	int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
830
831	memset(buf, '\0', sizeof(buf));
832
833	xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
834			   xsinfo->reqid, xsinfo->family, force_spi, fp,
835			   prefix, title);
836
837	if (prefix)
838		STRBUF_CAT(buf, prefix);
839	STRBUF_CAT(buf, "\t");
840
841	fputs(buf, fp);
842	fprintf(fp, "replay-window %u ", xsinfo->replay_window);
843	if (show_stats > 0)
844		fprintf(fp, "seq 0x%08u ", xsinfo->seq);
845	if (show_stats > 0 || xsinfo->flags) {
846		__u8 flags = xsinfo->flags;
847
848		fprintf(fp, "flag ");
849		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
850		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
851		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
852		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
853		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp");
854		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec");
855		XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ALIGN4, "align4");
856		if (flags)
857			fprintf(fp, "%x", flags);
858	}
859	if (show_stats > 0)
860		fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
861	fprintf(fp, "%s", _SL_);
862
863	xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
864
865	if (!xfrm_selector_iszero(&xsinfo->sel)) {
866		char sbuf[STRBUF_SIZE];
867
868		memcpy(sbuf, buf, sizeof(sbuf));
869		STRBUF_CAT(sbuf, "sel ");
870
871		xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
872	}
873
874	if (show_stats > 0) {
875		xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
876		xfrm_stats_print(&xsinfo->stats, fp, buf);
877	}
878
879	if (tb[XFRMA_SEC_CTX]) {
880		struct xfrm_user_sec_ctx *sctx;
881
882		fprintf(fp, "\tsecurity context ");
883
884		if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
885			fprintf(fp, "(ERROR truncated)");
886
887		sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
888
889		fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_);
890	}
891
892}
893
894void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
895			    struct rtattr *tb[], FILE *fp, const char *prefix,
896			    const char *title)
897{
898	char buf[STRBUF_SIZE];
899
900	memset(buf, '\0', sizeof(buf));
901
902	xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
903
904	if (tb[XFRMA_SEC_CTX]) {
905		struct xfrm_user_sec_ctx *sctx;
906
907		fprintf(fp, "\tsecurity context ");
908
909		if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
910			fprintf(fp, "(ERROR truncated)");
911
912		sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
913
914		fprintf(fp, "%s ", (char *)(sctx + 1));
915		fprintf(fp, "%s", _SL_);
916	}
917
918	if (prefix)
919		STRBUF_CAT(buf, prefix);
920	STRBUF_CAT(buf, "\t");
921
922	fputs(buf, fp);
923	if (xpinfo->dir >= XFRM_POLICY_MAX) {
924		xpinfo->dir -= XFRM_POLICY_MAX;
925		fprintf(fp, "socket ");
926	} else
927		fprintf(fp, "dir ");
928
929	switch (xpinfo->dir) {
930	case XFRM_POLICY_IN:
931		fprintf(fp, "in");
932		break;
933	case XFRM_POLICY_OUT:
934		fprintf(fp, "out");
935		break;
936	case XFRM_POLICY_FWD:
937		fprintf(fp, "fwd");
938		break;
939	default:
940		fprintf(fp, "%u", xpinfo->dir);
941		break;
942	}
943	fprintf(fp, " ");
944
945	switch (xpinfo->action) {
946	case XFRM_POLICY_ALLOW:
947		if (show_stats > 0)
948			fprintf(fp, "action allow ");
949		break;
950	case XFRM_POLICY_BLOCK:
951		fprintf(fp, "action block ");
952		break;
953	default:
954		fprintf(fp, "action %u ", xpinfo->action);
955		break;
956	}
957
958	if (show_stats)
959		fprintf(fp, "index %u ", xpinfo->index);
960	fprintf(fp, "priority %u ", xpinfo->priority);
961
962	if (tb[XFRMA_POLICY_TYPE]) {
963		struct xfrm_userpolicy_type *upt;
964
965		fprintf(fp, "ptype ");
966
967		if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
968			fprintf(fp, "(ERROR truncated)");
969
970		upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
971		fprintf(fp, "%s ", strxf_ptype(upt->type));
972	}
973
974	if (show_stats > 0)
975		fprintf(fp, "share %s ", strxf_share(xpinfo->share));
976
977	if (show_stats > 0 || xpinfo->flags) {
978		__u8 flags = xpinfo->flags;
979
980		fprintf(fp, "flag ");
981		XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
982		XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_ICMP, "icmp");
983		if (flags)
984			fprintf(fp, "%x", flags);
985	}
986	if (show_stats > 0)
987		fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
988	fprintf(fp, "%s", _SL_);
989
990	if (show_stats > 0)
991		xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
992
993	xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
994}
995
996int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
997		  int loose, int *argcp, char ***argvp)
998{
999	int argc = *argcp;
1000	char **argv = *argvp;
1001	inet_prefix dst;
1002	inet_prefix src;
1003
1004	memset(&dst, 0, sizeof(dst));
1005	memset(&src, 0, sizeof(src));
1006
1007	while (1) {
1008		if (strcmp(*argv, "src") == 0) {
1009			NEXT_ARG();
1010
1011			get_prefix(&src, *argv, preferred_family);
1012			if (src.family == AF_UNSPEC)
1013				invarg("\"src\" address family is AF_UNSPEC", *argv);
1014			if (family)
1015				*family = src.family;
1016
1017			memcpy(saddr, &src.data, sizeof(*saddr));
1018
1019			filter.id_src_mask = src.bitlen;
1020
1021		} else if (strcmp(*argv, "dst") == 0) {
1022			NEXT_ARG();
1023
1024			get_prefix(&dst, *argv, preferred_family);
1025			if (dst.family == AF_UNSPEC)
1026				invarg("\"dst\" address family is AF_UNSPEC", *argv);
1027			if (family)
1028				*family = dst.family;
1029
1030			memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
1031
1032			filter.id_dst_mask = dst.bitlen;
1033
1034		} else if (strcmp(*argv, "proto") == 0) {
1035			int ret;
1036
1037			NEXT_ARG();
1038
1039			ret = xfrm_xfrmproto_getbyname(*argv);
1040			if (ret < 0)
1041				invarg("\"XFRM-PROTO\" is invalid", *argv);
1042
1043			id->proto = (__u8)ret;
1044
1045			filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
1046
1047		} else if (strcmp(*argv, "spi") == 0) {
1048			__u32 spi;
1049
1050			NEXT_ARG();
1051			if (get_u32(&spi, *argv, 0))
1052				invarg("\"SPI\" is invalid", *argv);
1053
1054			spi = htonl(spi);
1055			id->spi = spi;
1056
1057			filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
1058
1059		} else {
1060			PREV_ARG(); /* back track */
1061			break;
1062		}
1063
1064		if (!NEXT_ARG_OK())
1065			break;
1066		NEXT_ARG();
1067	}
1068
1069	if (src.family && dst.family && (src.family != dst.family))
1070		invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1071
1072	if (id->spi && id->proto) {
1073		if (xfrm_xfrmproto_is_ro(id->proto)) {
1074			fprintf(stderr, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1075			        strxf_xfrmproto(id->proto));
1076			exit(1);
1077		} else if (id->proto == IPPROTO_COMP && ntohl(id->spi) >= 0x10000) {
1078			fprintf(stderr, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1079			        strxf_xfrmproto(id->proto));
1080			exit(1);
1081		}
1082	}
1083
1084	if (loose == 0 && id->proto == 0)
1085		missarg("XFRM-PROTO");
1086	if (argc == *argcp)
1087		missarg("ID");
1088
1089	*argcp = argc;
1090	*argvp = argv;
1091
1092	return 0;
1093}
1094
1095int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1096{
1097	int argc = *argcp;
1098	char **argv = *argvp;
1099
1100	if (matches(*argv, "transport") == 0)
1101		*mode = XFRM_MODE_TRANSPORT;
1102	else if (matches(*argv, "tunnel") == 0)
1103		*mode = XFRM_MODE_TUNNEL;
1104	else if (matches(*argv, "ro") == 0)
1105		*mode = XFRM_MODE_ROUTEOPTIMIZATION;
1106	else if (matches(*argv, "in_trigger") == 0)
1107		*mode = XFRM_MODE_IN_TRIGGER;
1108	else if (matches(*argv, "beet") == 0)
1109		*mode = XFRM_MODE_BEET;
1110	else
1111		invarg("\"MODE\" is invalid", *argv);
1112
1113	*argcp = argc;
1114	*argvp = argv;
1115
1116	return 0;
1117}
1118
1119int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1120{
1121	int argc = *argcp;
1122	char **argv = *argvp;
1123
1124	if (strcmp(*argv, "espinudp-nonike") == 0)
1125		*type = 1;
1126	else if (strcmp(*argv, "espinudp") == 0)
1127		*type = 2;
1128	else
1129		invarg("\"ENCAP-TYPE\" is invalid", *argv);
1130
1131	*argcp = argc;
1132	*argvp = argv;
1133
1134	return 0;
1135}
1136
1137/* NOTE: reqid is used by host-byte order */
1138int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1139{
1140	int argc = *argcp;
1141	char **argv = *argvp;
1142
1143	if (get_u32(reqid, *argv, 0))
1144		invarg("\"REQID\" is invalid", *argv);
1145
1146	*argcp = argc;
1147	*argvp = argv;
1148
1149	return 0;
1150}
1151
1152static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1153				      int *argcp, char ***argvp)
1154{
1155	int argc = *argcp;
1156	char **argv = *argvp;
1157	char *sportp = NULL;
1158	char *dportp = NULL;
1159	char *typep = NULL;
1160	char *codep = NULL;
1161	char *grekey = NULL;
1162
1163	while (1) {
1164		if (strcmp(*argv, "proto") == 0) {
1165			__u8 upspec;
1166
1167			NEXT_ARG();
1168
1169			if (strcmp(*argv, "any") == 0)
1170				upspec = 0;
1171			else {
1172				struct protoent *pp;
1173				pp = getprotobyname(*argv);
1174				if (pp)
1175					upspec = pp->p_proto;
1176				else {
1177					if (get_u8(&upspec, *argv, 0))
1178						invarg("\"PROTO\" is invalid", *argv);
1179				}
1180			}
1181			sel->proto = upspec;
1182
1183			filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1184
1185		} else if (strcmp(*argv, "sport") == 0) {
1186			sportp = *argv;
1187
1188			NEXT_ARG();
1189
1190			if (get_u16(&sel->sport, *argv, 0))
1191				invarg("\"PORT\" is invalid", *argv);
1192			sel->sport = htons(sel->sport);
1193			if (sel->sport)
1194				sel->sport_mask = ~((__u16)0);
1195
1196			filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1197
1198		} else if (strcmp(*argv, "dport") == 0) {
1199			dportp = *argv;
1200
1201			NEXT_ARG();
1202
1203			if (get_u16(&sel->dport, *argv, 0))
1204				invarg("\"PORT\" is invalid", *argv);
1205			sel->dport = htons(sel->dport);
1206			if (sel->dport)
1207				sel->dport_mask = ~((__u16)0);
1208
1209			filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1210
1211		} else if (strcmp(*argv, "type") == 0) {
1212			typep = *argv;
1213
1214			NEXT_ARG();
1215
1216			if (get_u16(&sel->sport, *argv, 0) ||
1217			    (sel->sport & ~((__u16)0xff)))
1218				invarg("\"type\" value is invalid", *argv);
1219			sel->sport = htons(sel->sport);
1220			sel->sport_mask = ~((__u16)0);
1221
1222			filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1223
1224
1225		} else if (strcmp(*argv, "code") == 0) {
1226			codep = *argv;
1227
1228			NEXT_ARG();
1229
1230			if (get_u16(&sel->dport, *argv, 0) ||
1231			    (sel->dport & ~((__u16)0xff)))
1232				invarg("\"code\" value is invalid", *argv);
1233			sel->dport = htons(sel->dport);
1234			sel->dport_mask = ~((__u16)0);
1235
1236			filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1237
1238		} else if (strcmp(*argv, "key") == 0) {
1239			unsigned uval;
1240
1241			grekey = *argv;
1242
1243			NEXT_ARG();
1244
1245			if (strchr(*argv, '.'))
1246				uval = htonl(get_addr32(*argv));
1247			else {
1248				if (get_unsigned(&uval, *argv, 0)<0) {
1249					fprintf(stderr, "invalid value for \"key\"; it should be an unsigned integer\n");
1250					exit(-1);
1251				}
1252			}
1253
1254			sel->sport = htons(uval >> 16);
1255			sel->dport = htons(uval & 0xffff);
1256			sel->sport_mask = ~((__u16)0);
1257			sel->dport_mask = ~((__u16)0);
1258
1259			filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1260
1261		} else {
1262			PREV_ARG(); /* back track */
1263			break;
1264		}
1265
1266		if (!NEXT_ARG_OK())
1267			break;
1268		NEXT_ARG();
1269	}
1270	if (argc == *argcp)
1271		missarg("UPSPEC");
1272	if (sportp || dportp) {
1273		switch (sel->proto) {
1274		case IPPROTO_TCP:
1275		case IPPROTO_UDP:
1276		case IPPROTO_SCTP:
1277		case IPPROTO_DCCP:
1278			break;
1279		default:
1280			fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1281			exit(1);
1282		}
1283	}
1284	if (typep || codep) {
1285		switch (sel->proto) {
1286		case IPPROTO_ICMP:
1287		case IPPROTO_ICMPV6:
1288		case IPPROTO_MH:
1289			break;
1290		default:
1291			fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1292			exit(1);
1293		}
1294	}
1295	if (grekey) {
1296		switch (sel->proto) {
1297		case IPPROTO_GRE:
1298			break;
1299		default:
1300			fprintf(stderr, "\"key\" is invalid with proto=%s\n", strxf_proto(sel->proto));
1301			exit(1);
1302		}
1303	}
1304
1305	*argcp = argc;
1306	*argvp = argv;
1307
1308	return 0;
1309}
1310
1311int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1312{
1313	int argc = *argcp;
1314	char **argv = *argvp;
1315	inet_prefix dst;
1316	inet_prefix src;
1317	char *upspecp = NULL;
1318
1319	memset(&dst, 0, sizeof(dst));
1320	memset(&src, 0, sizeof(src));
1321
1322	while (1) {
1323		if (strcmp(*argv, "src") == 0) {
1324			NEXT_ARG();
1325
1326			get_prefix(&src, *argv, preferred_family);
1327			if (src.family == AF_UNSPEC)
1328				invarg("\"src\" address family is AF_UNSPEC", *argv);
1329			sel->family = src.family;
1330
1331			memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1332			sel->prefixlen_s = src.bitlen;
1333
1334			filter.sel_src_mask = src.bitlen;
1335
1336		} else if (strcmp(*argv, "dst") == 0) {
1337			NEXT_ARG();
1338
1339			get_prefix(&dst, *argv, preferred_family);
1340			if (dst.family == AF_UNSPEC)
1341				invarg("\"dst\" address family is AF_UNSPEC", *argv);
1342			sel->family = dst.family;
1343
1344			memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1345			sel->prefixlen_d = dst.bitlen;
1346
1347			filter.sel_dst_mask = dst.bitlen;
1348
1349		} else if (strcmp(*argv, "dev") == 0) {
1350			int ifindex;
1351
1352			NEXT_ARG();
1353
1354			if (strcmp(*argv, "none") == 0)
1355				ifindex = 0;
1356			else {
1357				ifindex = ll_name_to_index(*argv);
1358				if (ifindex <= 0)
1359					invarg("\"DEV\" is invalid", *argv);
1360			}
1361			sel->ifindex = ifindex;
1362
1363			filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1364
1365		} else {
1366			if (upspecp) {
1367				PREV_ARG(); /* back track */
1368				break;
1369			} else {
1370				upspecp = *argv;
1371				xfrm_selector_upspec_parse(sel, &argc, &argv);
1372			}
1373		}
1374
1375		if (!NEXT_ARG_OK())
1376			break;
1377
1378		NEXT_ARG();
1379	}
1380
1381	if (src.family && dst.family && (src.family != dst.family))
1382		invarg("the same address family is required between \"src\" and \"dst\"", *argv);
1383
1384	if (argc == *argcp)
1385		missarg("SELECTOR");
1386
1387	*argcp = argc;
1388	*argvp = argv;
1389
1390	return 0;
1391}
1392
1393int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1394			    int *argcp, char ***argvp)
1395{
1396	int argc = *argcp;
1397	char **argv = *argvp;
1398	int ret;
1399
1400	if (strcmp(*argv, "time-soft") == 0) {
1401		NEXT_ARG();
1402		ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1403		if (ret)
1404			invarg("\"time-soft\" value is invalid", *argv);
1405	} else if (strcmp(*argv, "time-hard") == 0) {
1406		NEXT_ARG();
1407		ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1408		if (ret)
1409			invarg("\"time-hard\" value is invalid", *argv);
1410	} else if (strcmp(*argv, "time-use-soft") == 0) {
1411		NEXT_ARG();
1412		ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1413		if (ret)
1414			invarg("\"time-use-soft\" value is invalid", *argv);
1415	} else if (strcmp(*argv, "time-use-hard") == 0) {
1416		NEXT_ARG();
1417		ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1418		if (ret)
1419			invarg("\"time-use-hard\" value is invalid", *argv);
1420	} else if (strcmp(*argv, "byte-soft") == 0) {
1421		NEXT_ARG();
1422		ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1423		if (ret)
1424			invarg("\"byte-soft\" value is invalid", *argv);
1425	} else if (strcmp(*argv, "byte-hard") == 0) {
1426		NEXT_ARG();
1427		ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1428		if (ret)
1429			invarg("\"byte-hard\" value is invalid", *argv);
1430	} else if (strcmp(*argv, "packet-soft") == 0) {
1431		NEXT_ARG();
1432		ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1433		if (ret)
1434			invarg("\"packet-soft\" value is invalid", *argv);
1435	} else if (strcmp(*argv, "packet-hard") == 0) {
1436		NEXT_ARG();
1437		ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1438		if (ret)
1439			invarg("\"packet-hard\" value is invalid", *argv);
1440	} else
1441		invarg("\"LIMIT\" is invalid", *argv);
1442
1443	*argcp = argc;
1444	*argvp = argv;
1445
1446	return 0;
1447}
1448
1449int do_xfrm(int argc, char **argv)
1450{
1451	memset(&filter, 0, sizeof(filter));
1452
1453	if (argc < 1)
1454		usage();
1455
1456	if (matches(*argv, "state") == 0 ||
1457	    matches(*argv, "sa") == 0)
1458		return do_xfrm_state(argc-1, argv+1);
1459	else if (matches(*argv, "policy") == 0)
1460		return do_xfrm_policy(argc-1, argv+1);
1461	else if (matches(*argv, "monitor") == 0)
1462		return do_xfrm_monitor(argc-1, argv+1);
1463	else if (matches(*argv, "help") == 0) {
1464		usage();
1465		fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);
1466		exit(-1);
1467	}
1468	usage();
1469}
1470