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