]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipxfrm.c
5.10.0
[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);
af27494d
AA
652 if (tb[XFRMA_SET_MARK_MASK]) {
653 __u32 mask = rta_getattr_u32(tb[XFRMA_SET_MARK_MASK]);
654 fprintf(fp, "/0x%x", mask);
655 }
2ecb61a0
SAK
656}
657
f6fd52e6
JHS
658int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp)
659{
660 int argc = *argcp;
661 char **argv = *argvp;
662
663 NEXT_ARG();
664 if (get_u32(&mark->v, *argv, 0)) {
e8740e42 665 invarg("MARK value is invalid\n", *argv);
f6fd52e6
JHS
666 }
667 if (argc > 1)
668 NEXT_ARG();
669 else { /* last entry on parse line */
670 mark->m = 0xffffffff;
671 goto done;
672 }
673
674 if (strcmp(*argv, "mask") == 0) {
675 NEXT_ARG();
676 if (get_u32(&mark->m, *argv, 0)) {
e8740e42 677 invarg("MASK value is invalid\n", *argv);
f6fd52e6
JHS
678 }
679 } else {
680 mark->m = 0xffffffff;
681 PREV_ARG();
682 }
683
684done:
685 *argcp = argc;
686 *argvp = argv;
687
688 return 0;
689}
690
bcf32819 691void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
a6af9f2e 692 FILE *fp, const char *prefix, bool nokeys)
c7699875 693{
f6fd52e6
JHS
694 if (tb[XFRMA_MARK]) {
695 struct rtattr *rta = tb[XFRMA_MARK];
84da4099 696 struct xfrm_mark *m = RTA_DATA(rta);
56f5daac 697
2ecb61a0
SAK
698 fprintf(fp, "\tmark %#x/%#x ", m->v, m->m);
699
700 if (tb[XFRMA_OUTPUT_MARK])
701 xfrm_output_mark_print(tb, fp);
702 fprintf(fp, "%s", _SL_);
703 } else if (tb[XFRMA_OUTPUT_MARK]) {
704 fprintf(fp, "\t");
705
706 xfrm_output_mark_print(tb, fp);
6f4cad91 707 fprintf(fp, "%s", _SL_);
f6fd52e6
JHS
708 }
709
f323f2a3 710 if (tb[XFRMA_ALG_AUTH] && !tb[XFRMA_ALG_AUTH_TRUNC]) {
bcf32819 711 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
56f5daac 712
a6af9f2e
BW
713 xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_AUTH, RTA_PAYLOAD(rta),
714 fp, prefix, nokeys);
bcf32819 715 }
c7699875 716
f323f2a3
ND
717 if (tb[XFRMA_ALG_AUTH_TRUNC]) {
718 struct rtattr *rta = tb[XFRMA_ALG_AUTH_TRUNC];
56f5daac 719
a6af9f2e
BW
720 xfrm_auth_trunc_print(RTA_DATA(rta), RTA_PAYLOAD(rta), fp,
721 prefix, nokeys);
f323f2a3
ND
722 }
723
1758a81f
HX
724 if (tb[XFRMA_ALG_AEAD]) {
725 struct rtattr *rta = tb[XFRMA_ALG_AEAD];
56f5daac 726
a6af9f2e
BW
727 xfrm_aead_print(RTA_DATA(rta), RTA_PAYLOAD(rta), fp, prefix,
728 nokeys);
1758a81f
HX
729 }
730
bcf32819 731 if (tb[XFRMA_ALG_CRYPT]) {
732 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
56f5daac 733
a6af9f2e
BW
734 xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_CRYPT,
735 RTA_PAYLOAD(rta), fp, prefix, nokeys);
bcf32819 736 }
eaa34ee3 737
bcf32819 738 if (tb[XFRMA_ALG_COMP]) {
739 struct rtattr *rta = tb[XFRMA_ALG_COMP];
56f5daac 740
a6af9f2e
BW
741 xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_COMP, RTA_PAYLOAD(rta),
742 fp, prefix, nokeys);
bcf32819 743 }
eaa34ee3 744
bcf32819 745 if (tb[XFRMA_ENCAP]) {
746 struct xfrm_encap_tmpl *e;
eaa34ee3 747
bcf32819 748 if (prefix)
9a73e17d 749 fputs(prefix, fp);
bcf32819 750 fprintf(fp, "encap ");
eaa34ee3 751
bcf32819 752 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
753 fprintf(fp, "(ERROR truncated)");
7809c616 754 fprintf(fp, "%s", _SL_);
bcf32819 755 return;
c7699875 756 }
84da4099 757 e = RTA_DATA(tb[XFRMA_ENCAP]);
bcf32819 758
5cf576d9
SH
759 fprintf(fp, "type ");
760 switch (e->encap_type) {
22aec426 761 case UDP_ENCAP_ESPINUDP_NON_IKE:
5cf576d9
SH
762 fprintf(fp, "espinudp-nonike ");
763 break;
22aec426 764 case UDP_ENCAP_ESPINUDP:
5cf576d9
SH
765 fprintf(fp, "espinudp ");
766 break;
22aec426
SD
767 case TCP_ENCAP_ESPINTCP:
768 fprintf(fp, "espintcp ");
769 break;
5cf576d9
SH
770 default:
771 fprintf(fp, "%u ", e->encap_type);
772 break;
773 }
bcf32819 774 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
775 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
776
bcf32819 777 fprintf(fp, "addr %s",
2e96d2cc 778 rt_addr_n2a(family, sizeof(e->encap_oa), &e->encap_oa));
bcf32819 779 fprintf(fp, "%s", _SL_);
780 }
781
782 if (tb[XFRMA_TMPL]) {
783 struct rtattr *rta = tb[XFRMA_TMPL];
56f5daac 784
84da4099 785 xfrm_tmpl_print(RTA_DATA(rta),
656111b2 786 RTA_PAYLOAD(rta), fp, prefix);
c7699875 787 }
7ea4f5d3
MN
788
789 if (tb[XFRMA_COADDR]) {
9f1370c0 790 const xfrm_address_t *coa;
7ea4f5d3
MN
791
792 if (prefix)
9a73e17d 793 fputs(prefix, fp);
7ea4f5d3
MN
794 fprintf(fp, "coa ");
795
9f1370c0 796 coa = RTA_DATA(tb[XFRMA_COADDR]);
7ea4f5d3
MN
797 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
798 fprintf(fp, "(ERROR truncated)");
799 fprintf(fp, "%s", _SL_);
800 return;
801 }
802
7ea4f5d3 803 fprintf(fp, "%s",
2e96d2cc 804 rt_addr_n2a(family, sizeof(*coa), coa));
7ea4f5d3
MN
805 fprintf(fp, "%s", _SL_);
806 }
807
808 if (tb[XFRMA_LASTUSED]) {
809 __u64 lastused;
810
811 if (prefix)
9a73e17d 812 fputs(prefix, fp);
7ea4f5d3
MN
813 fprintf(fp, "lastused ");
814
815 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
816 fprintf(fp, "(ERROR truncated)");
817 fprintf(fp, "%s", _SL_);
818 return;
819 }
820
ff24746c 821 lastused = rta_getattr_u64(tb[XFRMA_LASTUSED]);
7ea4f5d3
MN
822
823 fprintf(fp, "%s", strxf_time(lastused));
824 fprintf(fp, "%s", _SL_);
825 }
f6fd52e6 826
0151b56d 827 if (tb[XFRMA_REPLAY_VAL]) {
828 struct xfrm_replay_state *replay;
829
830 if (prefix)
831 fputs(prefix, fp);
832 fprintf(fp, "anti-replay context: ");
833
834 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_VAL]) < sizeof(*replay)) {
835 fprintf(fp, "(ERROR truncated)");
836 fprintf(fp, "%s", _SL_);
837 return;
838 }
839
9f1370c0 840 replay = RTA_DATA(tb[XFRMA_REPLAY_VAL]);
0151b56d 841 fprintf(fp, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
842 replay->seq, replay->oseq, replay->bitmap);
843 fprintf(fp, "%s", _SL_);
844 }
845
846 if (tb[XFRMA_REPLAY_ESN_VAL]) {
847 struct xfrm_replay_state_esn *replay;
848 unsigned int i, j;
849
850 if (prefix)
851 fputs(prefix, fp);
852 fprintf(fp, "anti-replay esn context:");
853
854 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_ESN_VAL]) < sizeof(*replay)) {
855 fprintf(fp, "(ERROR truncated)");
856 fprintf(fp, "%s", _SL_);
857 return;
858 }
859 fprintf(fp, "%s", _SL_);
860
9f1370c0 861 replay = RTA_DATA(tb[XFRMA_REPLAY_ESN_VAL]);
0151b56d 862 if (prefix)
863 fputs(prefix, fp);
864 fprintf(fp, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
865 replay->seq_hi, replay->seq, replay->oseq_hi,
866 replay->oseq);
867 fprintf(fp, "%s", _SL_);
868 if (prefix)
869 fputs(prefix, fp);
870 fprintf(fp, " replay_window %u, bitmap-length %u",
871 replay->replay_window, replay->bmp_len);
872 for (i = replay->bmp_len, j = 0; i; i--) {
873 if (j++ % 8 == 0) {
874 fprintf(fp, "%s", _SL_);
875 if (prefix)
876 fputs(prefix, fp);
877 fprintf(fp, " ");
878 }
879 fprintf(fp, "%08x ", replay->bmp[i - 1]);
880 }
881 fprintf(fp, "%s", _SL_);
882 }
cfd2e727
BP
883 if (tb[XFRMA_OFFLOAD_DEV]) {
884 struct xfrm_user_offload *xuo;
885
886 if (prefix)
887 fputs(prefix, fp);
888 fprintf(fp, "crypto offload parameters: ");
889
890 if (RTA_PAYLOAD(tb[XFRMA_OFFLOAD_DEV]) < sizeof(*xuo)) {
891 fprintf(fp, "(ERROR truncated)");
892 fprintf(fp, "%s", _SL_);
893 return;
894 }
895
896 xuo = (struct xfrm_user_offload *)
897 RTA_DATA(tb[XFRMA_OFFLOAD_DEV]);
898 fprintf(fp, "dev %s dir %s", ll_index_to_name(xuo->ifindex),
899 (xuo->flags & XFRM_OFFLOAD_INBOUND) ? "in" : "out");
900 fprintf(fp, "%s", _SL_);
901 }
aed63ae1
EB
902 if (tb[XFRMA_IF_ID]) {
903 __u32 if_id = rta_getattr_u32(tb[XFRMA_IF_ID]);
904
905 if (prefix)
906 fputs(prefix, fp);
907 fprintf(fp, "if_id %#x", if_id);
908 fprintf(fp, "%s", _SL_);
909 }
c7699875 910}
911
15ac4cdc 912static int xfrm_selector_iszero(struct xfrm_selector *s)
913{
d17b136f 914 struct xfrm_selector s0 = {};
15ac4cdc 915
916 return (memcmp(&s0, s, sizeof(s0)) == 0);
917}
918
919void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
920 struct rtattr *tb[], FILE *fp, const char *prefix,
a6af9f2e 921 const char *title, bool nokeys)
15ac4cdc 922{
d17b136f 923 char buf[STRBUF_SIZE] = {};
7ea4f5d3 924 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
15ac4cdc 925
15ac4cdc 926 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
7ea4f5d3
MN
927 xsinfo->reqid, xsinfo->family, force_spi, fp,
928 prefix, title);
15ac4cdc 929
930 if (prefix)
44cc6c79
PS
931 strlcat(buf, prefix, sizeof(buf));
932 strlcat(buf, "\t", sizeof(buf));
15ac4cdc 933
9a73e17d 934 fputs(buf, fp);
15ac4cdc 935 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
936 if (show_stats > 0)
937 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
938 if (show_stats > 0 || xsinfo->flags) {
939 __u8 flags = xsinfo->flags;
940
941 fprintf(fp, "flag ");
942 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
943 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
c1fa2253 944 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
7ea4f5d3 945 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
15bb82c6
AB
946 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp");
947 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec");
98f5519c 948 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ALIGN4, "align4");
0151b56d 949 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ESN, "esn");
15ac4cdc 950 if (flags)
951 fprintf(fp, "%x", flags);
15ac4cdc 952 }
1ed509bb 953 if (show_stats > 0 && tb[XFRMA_SA_EXTRA_FLAGS]) {
9f1370c0 954 __u32 extra_flags = rta_getattr_u32(tb[XFRMA_SA_EXTRA_FLAGS]);
dc8867d0
ND
955
956 fprintf(fp, "extra_flag ");
957 XFRM_FLAG_PRINT(fp, extra_flags,
958 XFRM_SA_XFLAG_DONT_ENCAP_DSCP,
959 "dont-encap-dscp");
a7f1974f
PV
960 XFRM_FLAG_PRINT(fp, extra_flags,
961 XFRM_SA_XFLAG_OSEQ_MAY_WRAP,
962 "oseq-may-wrap");
dc8867d0
ND
963 if (extra_flags)
964 fprintf(fp, "%x", extra_flags);
965 }
c1fa2253
MN
966 if (show_stats > 0)
967 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
15ac4cdc 968 fprintf(fp, "%s", _SL_);
969
a6af9f2e 970 xfrm_xfrma_print(tb, xsinfo->family, fp, buf, nokeys);
15ac4cdc 971
972 if (!xfrm_selector_iszero(&xsinfo->sel)) {
973 char sbuf[STRBUF_SIZE];
974
975 memcpy(sbuf, buf, sizeof(sbuf));
44cc6c79 976 strlcat(sbuf, "sel ", sizeof(sbuf));
15ac4cdc 977
978 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
979 }
980
981 if (show_stats > 0) {
982 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
983 xfrm_stats_print(&xsinfo->stats, fp, buf);
984 }
b2bb289a
JL
985
986 if (tb[XFRMA_SEC_CTX]) {
987 struct xfrm_user_sec_ctx *sctx;
988
989 fprintf(fp, "\tsecurity context ");
990
991 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
992 fprintf(fp, "(ERROR truncated)");
993
9f1370c0 994 sctx = RTA_DATA(tb[XFRMA_SEC_CTX]);
b2bb289a
JL
995
996 fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_);
997 }
998
15ac4cdc 999}
1000
1001void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
1002 struct rtattr *tb[], FILE *fp, const char *prefix,
1003 const char *title)
1004{
d17b136f 1005 char buf[STRBUF_SIZE] = {};
15ac4cdc 1006
1007 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
1008
b2bb289a
JL
1009 if (tb[XFRMA_SEC_CTX]) {
1010 struct xfrm_user_sec_ctx *sctx;
1011
1012 fprintf(fp, "\tsecurity context ");
1013
1014 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
1015 fprintf(fp, "(ERROR truncated)");
1016
9f1370c0 1017 sctx = RTA_DATA(tb[XFRMA_SEC_CTX]);
b2bb289a
JL
1018
1019 fprintf(fp, "%s ", (char *)(sctx + 1));
1020 fprintf(fp, "%s", _SL_);
1021 }
1022
15ac4cdc 1023 if (prefix)
44cc6c79
PS
1024 strlcat(buf, prefix, sizeof(buf));
1025 strlcat(buf, "\t", sizeof(buf));
15ac4cdc 1026
9a73e17d 1027 fputs(buf, fp);
66abc090
UW
1028 if (xpinfo->dir >= XFRM_POLICY_MAX) {
1029 xpinfo->dir -= XFRM_POLICY_MAX;
1030 fprintf(fp, "socket ");
1031 } else
1032 fprintf(fp, "dir ");
1033
15ac4cdc 1034 switch (xpinfo->dir) {
1035 case XFRM_POLICY_IN:
1036 fprintf(fp, "in");
1037 break;
1038 case XFRM_POLICY_OUT:
1039 fprintf(fp, "out");
1040 break;
1041 case XFRM_POLICY_FWD:
1042 fprintf(fp, "fwd");
1043 break;
1044 default:
1045 fprintf(fp, "%u", xpinfo->dir);
1046 break;
1047 }
1048 fprintf(fp, " ");
1049
1050 switch (xpinfo->action) {
1051 case XFRM_POLICY_ALLOW:
1052 if (show_stats > 0)
1053 fprintf(fp, "action allow ");
1054 break;
1055 case XFRM_POLICY_BLOCK:
1056 fprintf(fp, "action block ");
1057 break;
1058 default:
1059 fprintf(fp, "action %u ", xpinfo->action);
1060 break;
1061 }
1062
1063 if (show_stats)
1064 fprintf(fp, "index %u ", xpinfo->index);
1065 fprintf(fp, "priority %u ", xpinfo->priority);
972938e9 1066
972938e9
MN
1067 if (tb[XFRMA_POLICY_TYPE]) {
1068 struct xfrm_userpolicy_type *upt;
1069
efe69c1b
MN
1070 fprintf(fp, "ptype ");
1071
972938e9
MN
1072 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
1073 fprintf(fp, "(ERROR truncated)");
1074
9f1370c0 1075 upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
efe69c1b 1076 fprintf(fp, "%s ", strxf_ptype(upt->type));
972938e9
MN
1077 }
1078
c1fa2253 1079 if (show_stats > 0)
15ac4cdc 1080 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
c1fa2253
MN
1081
1082 if (show_stats > 0 || xpinfo->flags) {
1083 __u8 flags = xpinfo->flags;
1084
1085 fprintf(fp, "flag ");
1086 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
c0635644 1087 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_ICMP, "icmp");
c1fa2253
MN
1088 if (flags)
1089 fprintf(fp, "%x", flags);
15ac4cdc 1090 }
c1fa2253
MN
1091 if (show_stats > 0)
1092 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
15ac4cdc 1093 fprintf(fp, "%s", _SL_);
1094
1095 if (show_stats > 0)
1096 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
1097
a6af9f2e 1098 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf, false);
15ac4cdc 1099}
1100
c7699875 1101int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
7809c616 1102 int loose, int *argcp, char ***argvp)
c7699875 1103{
1104 int argc = *argcp;
1105 char **argv = *argvp;
d17b136f
PS
1106 inet_prefix dst = {};
1107 inet_prefix src = {};
c7699875 1108
1109 while (1) {
1110 if (strcmp(*argv, "src") == 0) {
1111 NEXT_ARG();
1112
1113 get_prefix(&src, *argv, preferred_family);
1114 if (src.family == AF_UNSPEC)
e8740e42 1115 invarg("value after \"src\" has an unrecognized address family", *argv);
c7699875 1116 if (family)
1117 *family = src.family;
1118
1119 memcpy(saddr, &src.data, sizeof(*saddr));
1120
1121 filter.id_src_mask = src.bitlen;
1122
1123 } else if (strcmp(*argv, "dst") == 0) {
1124 NEXT_ARG();
1125
1126 get_prefix(&dst, *argv, preferred_family);
1127 if (dst.family == AF_UNSPEC)
e8740e42 1128 invarg("value after \"dst\" has an unrecognized address family", *argv);
c7699875 1129 if (family)
1130 *family = dst.family;
1131
1132 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
1133
1134 filter.id_dst_mask = dst.bitlen;
1135
1136 } else if (strcmp(*argv, "proto") == 0) {
29aa4dd7 1137 int ret;
c7699875 1138
1139 NEXT_ARG();
1140
29aa4dd7 1141 ret = xfrm_xfrmproto_getbyname(*argv);
1142 if (ret < 0)
e8740e42 1143 invarg("XFRM-PROTO value is invalid", *argv);
c7699875 1144
29aa4dd7 1145 id->proto = (__u8)ret;
c7699875 1146
1147 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
1148
1149 } else if (strcmp(*argv, "spi") == 0) {
c7699875 1150 NEXT_ARG();
9f7401fa 1151 if (get_be32(&id->spi, *argv, 0))
e8740e42 1152 invarg("SPI value is invalid", *argv);
c7699875 1153
c7699875 1154 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
1155
1156 } else {
1157 PREV_ARG(); /* back track */
1158 break;
1159 }
1160
1161 if (!NEXT_ARG_OK())
1162 break;
1163 NEXT_ARG();
1164 }
1165
1166 if (src.family && dst.family && (src.family != dst.family))
e8740e42 1167 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
c7699875 1168
1d26e1fe
DW
1169 if (id->spi && id->proto) {
1170 if (xfrm_xfrmproto_is_ro(id->proto)) {
1171 fprintf(stderr, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
56f5daac 1172 strxf_xfrmproto(id->proto));
1d26e1fe
DW
1173 exit(1);
1174 } else if (id->proto == IPPROTO_COMP && ntohl(id->spi) >= 0x10000) {
1175 fprintf(stderr, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
56f5daac 1176 strxf_xfrmproto(id->proto));
1d26e1fe
DW
1177 exit(1);
1178 }
1179 }
1180
29aa4dd7 1181 if (loose == 0 && id->proto == 0)
cbec0219 1182 missarg("XFRM-PROTO");
c7699875 1183 if (argc == *argcp)
1184 missarg("ID");
1185
1186 *argcp = argc;
1187 *argvp = argv;
1188
1189 return 0;
1190}
1191
1192int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1193{
1194 int argc = *argcp;
1195 char **argv = *argvp;
1196
1197 if (matches(*argv, "transport") == 0)
7ea4f5d3 1198 *mode = XFRM_MODE_TRANSPORT;
c7699875 1199 else if (matches(*argv, "tunnel") == 0)
7ea4f5d3
MN
1200 *mode = XFRM_MODE_TUNNEL;
1201 else if (matches(*argv, "ro") == 0)
1202 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
1203 else if (matches(*argv, "in_trigger") == 0)
1204 *mode = XFRM_MODE_IN_TRIGGER;
34e099e2 1205 else if (matches(*argv, "beet") == 0)
7ea4f5d3 1206 *mode = XFRM_MODE_BEET;
c7699875 1207 else
e8740e42 1208 invarg("MODE value is invalid", *argv);
c7699875 1209
1210 *argcp = argc;
1211 *argvp = argv;
1212
1213 return 0;
1214}
1215
5cf576d9
SH
1216int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1217{
1218 int argc = *argcp;
1219 char **argv = *argvp;
1220
1221 if (strcmp(*argv, "espinudp-nonike") == 0)
22aec426 1222 *type = UDP_ENCAP_ESPINUDP_NON_IKE;
5cf576d9 1223 else if (strcmp(*argv, "espinudp") == 0)
22aec426
SD
1224 *type = UDP_ENCAP_ESPINUDP;
1225 else if (strcmp(*argv, "espintcp") == 0)
1226 *type = TCP_ENCAP_ESPINTCP;
5cf576d9 1227 else
e8740e42 1228 invarg("ENCAP-TYPE value is invalid", *argv);
5cf576d9
SH
1229
1230 *argcp = argc;
1231 *argvp = argv;
1232
1233 return 0;
1234}
1235
c7699875 1236/* NOTE: reqid is used by host-byte order */
1237int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1238{
1239 int argc = *argcp;
1240 char **argv = *argvp;
1241
1242 if (get_u32(reqid, *argv, 0))
e8740e42 1243 invarg("REQID value is invalid", *argv);
c7699875 1244
1245 *argcp = argc;
1246 *argvp = argv;
1247
1248 return 0;
1249}
1250
1251static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1252 int *argcp, char ***argvp)
1253{
1254 int argc = *argcp;
1255 char **argv = *argvp;
c70b36d2 1256 char *sportp = NULL;
1257 char *dportp = NULL;
1258 char *typep = NULL;
1259 char *codep = NULL;
4a9608e6 1260 char *grekey = NULL;
c7699875 1261
1262 while (1) {
1263 if (strcmp(*argv, "proto") == 0) {
7809c616 1264 __u8 upspec;
1265
c7699875 1266 NEXT_ARG();
1267
1268 if (strcmp(*argv, "any") == 0)
1269 upspec = 0;
1270 else {
1271 struct protoent *pp;
56f5daac 1272
c7699875 1273 pp = getprotobyname(*argv);
1274 if (pp)
1275 upspec = pp->p_proto;
1276 else {
1277 if (get_u8(&upspec, *argv, 0))
e8740e42 1278 invarg("PROTO value is invalid", *argv);
c7699875 1279 }
1280 }
1281 sel->proto = upspec;
1282
1283 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1284
1285 } else if (strcmp(*argv, "sport") == 0) {
c70b36d2 1286 sportp = *argv;
1287
c7699875 1288 NEXT_ARG();
1289
9f7401fa 1290 if (get_be16(&sel->sport, *argv, 0))
e8740e42 1291 invarg("value after \"sport\" is invalid", *argv);
c7699875 1292 if (sel->sport)
1293 sel->sport_mask = ~((__u16)0);
1294
1295 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1296
1297 } else if (strcmp(*argv, "dport") == 0) {
c70b36d2 1298 dportp = *argv;
1299
c7699875 1300 NEXT_ARG();
1301
9f7401fa 1302 if (get_be16(&sel->dport, *argv, 0))
e8740e42 1303 invarg("value after \"dport\" is invalid", *argv);
c7699875 1304 if (sel->dport)
1305 sel->dport_mask = ~((__u16)0);
1306
1307 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1308
c70b36d2 1309 } else if (strcmp(*argv, "type") == 0) {
1310 typep = *argv;
1311
1312 NEXT_ARG();
1313
1314 if (get_u16(&sel->sport, *argv, 0) ||
1315 (sel->sport & ~((__u16)0xff)))
e8740e42 1316 invarg("value after \"type\" is invalid", *argv);
c70b36d2 1317 sel->sport = htons(sel->sport);
1318 sel->sport_mask = ~((__u16)0);
1319
1320 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1321
1322
1323 } else if (strcmp(*argv, "code") == 0) {
1324 codep = *argv;
1325
1326 NEXT_ARG();
1327
1328 if (get_u16(&sel->dport, *argv, 0) ||
1329 (sel->dport & ~((__u16)0xff)))
e8740e42 1330 invarg("value after \"code\" is invalid", *argv);
c70b36d2 1331 sel->dport = htons(sel->dport);
1332 sel->dport_mask = ~((__u16)0);
1333
1334 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1335
4a9608e6 1336 } else if (strcmp(*argv, "key") == 0) {
56f5daac 1337 unsigned int uval;
4a9608e6
TT
1338
1339 grekey = *argv;
1340
1341 NEXT_ARG();
1342
1343 if (strchr(*argv, '.'))
1344 uval = htonl(get_addr32(*argv));
1345 else {
56f5daac 1346 if (get_unsigned(&uval, *argv, 0) < 0) {
e8740e42 1347 fprintf(stderr, "value after \"key\" is invalid\n");
4a9608e6
TT
1348 exit(-1);
1349 }
1350 }
1351
1352 sel->sport = htons(uval >> 16);
1353 sel->dport = htons(uval & 0xffff);
1354 sel->sport_mask = ~((__u16)0);
1355 sel->dport_mask = ~((__u16)0);
1356
1357 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1358
c7699875 1359 } else {
1360 PREV_ARG(); /* back track */
1361 break;
1362 }
1363
1364 if (!NEXT_ARG_OK())
1365 break;
1366 NEXT_ARG();
1367 }
1368 if (argc == *argcp)
1369 missarg("UPSPEC");
c70b36d2 1370 if (sportp || dportp) {
1371 switch (sel->proto) {
1372 case IPPROTO_TCP:
1373 case IPPROTO_UDP:
1374 case IPPROTO_SCTP:
27356a5e 1375 case IPPROTO_DCCP:
11a3e5c4 1376 case IPPROTO_IP: /* to allow shared SA for different protocols */
c70b36d2 1377 break;
1378 default:
e8740e42 1379 fprintf(stderr, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
c70b36d2 1380 exit(1);
1381 }
1382 }
1383 if (typep || codep) {
1384 switch (sel->proto) {
1385 case IPPROTO_ICMP:
1386 case IPPROTO_ICMPV6:
0bf0fbc4 1387 case IPPROTO_MH:
c70b36d2 1388 break;
1389 default:
e8740e42 1390 fprintf(stderr, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
c70b36d2 1391 exit(1);
1392 }
1393 }
4a9608e6
TT
1394 if (grekey) {
1395 switch (sel->proto) {
1396 case IPPROTO_GRE:
1397 break;
1398 default:
e8740e42 1399 fprintf(stderr, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
4a9608e6
TT
1400 exit(1);
1401 }
1402 }
c7699875 1403
1404 *argcp = argc;
1405 *argvp = argv;
1406
1407 return 0;
1408}
1409
1410int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1411{
1412 int argc = *argcp;
1413 char **argv = *argvp;
d17b136f
PS
1414 inet_prefix dst = {};
1415 inet_prefix src = {};
7809c616 1416 char *upspecp = NULL;
c7699875 1417
c7699875 1418 while (1) {
1419 if (strcmp(*argv, "src") == 0) {
1420 NEXT_ARG();
1421
1422 get_prefix(&src, *argv, preferred_family);
1423 if (src.family == AF_UNSPEC)
e8740e42 1424 invarg("value after \"src\" has an unrecognized address family", *argv);
c7699875 1425 sel->family = src.family;
1426
1427 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1428 sel->prefixlen_s = src.bitlen;
1429
1430 filter.sel_src_mask = src.bitlen;
1431
1432 } else if (strcmp(*argv, "dst") == 0) {
1433 NEXT_ARG();
1434
1435 get_prefix(&dst, *argv, preferred_family);
1436 if (dst.family == AF_UNSPEC)
e8740e42 1437 invarg("value after \"dst\" has an unrecognized address family", *argv);
c7699875 1438 sel->family = dst.family;
1439
1440 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1441 sel->prefixlen_d = dst.bitlen;
1442
1443 filter.sel_dst_mask = dst.bitlen;
1444
c7699875 1445 } else if (strcmp(*argv, "dev") == 0) {
1446 int ifindex;
1447
1448 NEXT_ARG();
1449
1450 if (strcmp(*argv, "none") == 0)
1451 ifindex = 0;
1452 else {
dcb283c3 1453 ifindex = ll_name_to_index(*argv);
c7699875 1454 if (ifindex <= 0)
e8740e42 1455 invarg("DEV value is invalid", *argv);
c7699875 1456 }
1457 sel->ifindex = ifindex;
1458
1459 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1460
1461 } else {
7809c616 1462 if (upspecp) {
1463 PREV_ARG(); /* back track */
1464 break;
1465 } else {
1466 upspecp = *argv;
1467 xfrm_selector_upspec_parse(sel, &argc, &argv);
1468 }
c7699875 1469 }
1470
1471 if (!NEXT_ARG_OK())
1472 break;
1473
1474 NEXT_ARG();
1475 }
1476
1477 if (src.family && dst.family && (src.family != dst.family))
e8740e42 1478 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
c7699875 1479
1480 if (argc == *argcp)
1481 missarg("SELECTOR");
1482
1483 *argcp = argc;
1484 *argvp = argv;
1485
1486 return 0;
1487}
1488
1489int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1490 int *argcp, char ***argvp)
1491{
1492 int argc = *argcp;
1493 char **argv = *argvp;
1494 int ret;
1495
1496 if (strcmp(*argv, "time-soft") == 0) {
1497 NEXT_ARG();
1498 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1499 if (ret)
e8740e42 1500 invarg("value after \"time-soft\" is invalid", *argv);
c7699875 1501 } else if (strcmp(*argv, "time-hard") == 0) {
1502 NEXT_ARG();
1503 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1504 if (ret)
e8740e42 1505 invarg("value after \"time-hard\" is invalid", *argv);
c7699875 1506 } else if (strcmp(*argv, "time-use-soft") == 0) {
1507 NEXT_ARG();
1508 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1509 if (ret)
e8740e42 1510 invarg("value after \"time-use-soft\" is invalid", *argv);
c7699875 1511 } else if (strcmp(*argv, "time-use-hard") == 0) {
1512 NEXT_ARG();
1513 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1514 if (ret)
e8740e42 1515 invarg("value after \"time-use-hard\" is invalid", *argv);
c7699875 1516 } else if (strcmp(*argv, "byte-soft") == 0) {
1517 NEXT_ARG();
1518 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1519 if (ret)
e8740e42 1520 invarg("value after \"byte-soft\" is invalid", *argv);
c7699875 1521 } else if (strcmp(*argv, "byte-hard") == 0) {
1522 NEXT_ARG();
1523 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1524 if (ret)
e8740e42 1525 invarg("value after \"byte-hard\" is invalid", *argv);
c7699875 1526 } else if (strcmp(*argv, "packet-soft") == 0) {
1527 NEXT_ARG();
1528 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1529 if (ret)
e8740e42 1530 invarg("value after \"packet-soft\" is invalid", *argv);
c7699875 1531 } else if (strcmp(*argv, "packet-hard") == 0) {
1532 NEXT_ARG();
1533 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1534 if (ret)
e8740e42 1535 invarg("value after \"packet-hard\" is invalid", *argv);
c7699875 1536 } else
e8740e42 1537 invarg("LIMIT value is invalid", *argv);
c7699875 1538
1539 *argcp = argc;
1540 *argvp = argv;
1541
1542 return 0;
1543}
1544
1545int do_xfrm(int argc, char **argv)
1546{
1547 memset(&filter, 0, sizeof(filter));
1548
1549 if (argc < 1)
1550 usage();
1551
ad273962 1552 if (matches(*argv, "state") == 0 ||
15ac4cdc 1553 matches(*argv, "sa") == 0)
c7699875 1554 return do_xfrm_state(argc-1, argv+1);
15ac4cdc 1555 else if (matches(*argv, "policy") == 0)
c7699875 1556 return do_xfrm_policy(argc-1, argv+1);
15ac4cdc 1557 else if (matches(*argv, "monitor") == 0)
1558 return do_xfrm_monitor(argc-1, argv+1);
ad273962 1559 else if (matches(*argv, "help") == 0) {
c7699875 1560 usage();
1561 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);
1562 exit(-1);
1563 }
1564 usage();
1565}