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