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