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