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