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