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