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