]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipxfrm.c
iproute2: act_ipt fix xtables breakage
[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,
cbec0219
DW
62 "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
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
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
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");
c0635644 984 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_ICMP, "icmp");
c1fa2253
MN
985 if (flags)
986 fprintf(fp, "%x", flags);
15ac4cdc 987 }
c1fa2253
MN
988 if (show_stats > 0)
989 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
15ac4cdc 990 fprintf(fp, "%s", _SL_);
991
992 if (show_stats > 0)
993 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
994
995 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
996}
997
c7699875 998int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
7809c616 999 int loose, int *argcp, char ***argvp)
c7699875 1000{
1001 int argc = *argcp;
1002 char **argv = *argvp;
1003 inet_prefix dst;
1004 inet_prefix src;
c7699875 1005
1006 memset(&dst, 0, sizeof(dst));
1007 memset(&src, 0, sizeof(src));
1008
1009 while (1) {
1010 if (strcmp(*argv, "src") == 0) {
1011 NEXT_ARG();
1012
1013 get_prefix(&src, *argv, preferred_family);
1014 if (src.family == AF_UNSPEC)
eaa34ee3 1015 invarg("\"src\" address family is AF_UNSPEC", *argv);
c7699875 1016 if (family)
1017 *family = src.family;
1018
1019 memcpy(saddr, &src.data, sizeof(*saddr));
1020
1021 filter.id_src_mask = src.bitlen;
1022
1023 } else if (strcmp(*argv, "dst") == 0) {
1024 NEXT_ARG();
1025
1026 get_prefix(&dst, *argv, preferred_family);
1027 if (dst.family == AF_UNSPEC)
eaa34ee3 1028 invarg("\"dst\" address family is AF_UNSPEC", *argv);
c7699875 1029 if (family)
1030 *family = dst.family;
1031
1032 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
1033
1034 filter.id_dst_mask = dst.bitlen;
1035
1036 } else if (strcmp(*argv, "proto") == 0) {
29aa4dd7 1037 int ret;
c7699875 1038
1039 NEXT_ARG();
1040
29aa4dd7 1041 ret = xfrm_xfrmproto_getbyname(*argv);
1042 if (ret < 0)
cbec0219 1043 invarg("\"XFRM-PROTO\" is invalid", *argv);
c7699875 1044
29aa4dd7 1045 id->proto = (__u8)ret;
c7699875 1046
1047 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
1048
1049 } else if (strcmp(*argv, "spi") == 0) {
1050 __u32 spi;
1051
1052 NEXT_ARG();
1053 if (get_u32(&spi, *argv, 0))
1054 invarg("\"SPI\" is invalid", *argv);
1055
1056 spi = htonl(spi);
1057 id->spi = spi;
1058
1059 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
1060
1061 } else {
1062 PREV_ARG(); /* back track */
1063 break;
1064 }
1065
1066 if (!NEXT_ARG_OK())
1067 break;
1068 NEXT_ARG();
1069 }
1070
1071 if (src.family && dst.family && (src.family != dst.family))
eaa34ee3 1072 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
c7699875 1073
29aa4dd7 1074 if (loose == 0 && id->proto == 0)
cbec0219 1075 missarg("XFRM-PROTO");
c7699875 1076 if (argc == *argcp)
1077 missarg("ID");
1078
1079 *argcp = argc;
1080 *argvp = argv;
1081
1082 return 0;
1083}
1084
1085int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1086{
1087 int argc = *argcp;
1088 char **argv = *argvp;
1089
1090 if (matches(*argv, "transport") == 0)
7ea4f5d3 1091 *mode = XFRM_MODE_TRANSPORT;
c7699875 1092 else if (matches(*argv, "tunnel") == 0)
7ea4f5d3
MN
1093 *mode = XFRM_MODE_TUNNEL;
1094 else if (matches(*argv, "ro") == 0)
1095 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
1096 else if (matches(*argv, "in_trigger") == 0)
1097 *mode = XFRM_MODE_IN_TRIGGER;
34e099e2 1098 else if (matches(*argv, "beet") == 0)
7ea4f5d3 1099 *mode = XFRM_MODE_BEET;
c7699875 1100 else
1101 invarg("\"MODE\" is invalid", *argv);
1102
1103 *argcp = argc;
1104 *argvp = argv;
1105
1106 return 0;
1107}
1108
5cf576d9
SH
1109int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1110{
1111 int argc = *argcp;
1112 char **argv = *argvp;
1113
1114 if (strcmp(*argv, "espinudp-nonike") == 0)
1115 *type = 1;
1116 else if (strcmp(*argv, "espinudp") == 0)
1117 *type = 2;
1118 else
1119 invarg("\"ENCAP-TYPE\" is invalid", *argv);
1120
1121 *argcp = argc;
1122 *argvp = argv;
1123
1124 return 0;
1125}
1126
c7699875 1127/* NOTE: reqid is used by host-byte order */
1128int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1129{
1130 int argc = *argcp;
1131 char **argv = *argvp;
1132
1133 if (get_u32(reqid, *argv, 0))
1134 invarg("\"REQID\" is invalid", *argv);
1135
1136 *argcp = argc;
1137 *argvp = argv;
1138
1139 return 0;
1140}
1141
1142static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1143 int *argcp, char ***argvp)
1144{
1145 int argc = *argcp;
1146 char **argv = *argvp;
c70b36d2 1147 char *sportp = NULL;
1148 char *dportp = NULL;
1149 char *typep = NULL;
1150 char *codep = NULL;
4a9608e6 1151 char *grekey = NULL;
c7699875 1152
1153 while (1) {
1154 if (strcmp(*argv, "proto") == 0) {
7809c616 1155 __u8 upspec;
1156
c7699875 1157 NEXT_ARG();
1158
1159 if (strcmp(*argv, "any") == 0)
1160 upspec = 0;
1161 else {
1162 struct protoent *pp;
1163 pp = getprotobyname(*argv);
1164 if (pp)
1165 upspec = pp->p_proto;
1166 else {
1167 if (get_u8(&upspec, *argv, 0))
7809c616 1168 invarg("\"PROTO\" is invalid", *argv);
c7699875 1169 }
1170 }
1171 sel->proto = upspec;
1172
1173 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1174
1175 } else if (strcmp(*argv, "sport") == 0) {
c70b36d2 1176 sportp = *argv;
1177
c7699875 1178 NEXT_ARG();
1179
1180 if (get_u16(&sel->sport, *argv, 0))
1181 invarg("\"PORT\" is invalid", *argv);
1182 sel->sport = htons(sel->sport);
1183 if (sel->sport)
1184 sel->sport_mask = ~((__u16)0);
1185
1186 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1187
1188 } else if (strcmp(*argv, "dport") == 0) {
c70b36d2 1189 dportp = *argv;
1190
c7699875 1191 NEXT_ARG();
1192
1193 if (get_u16(&sel->dport, *argv, 0))
1194 invarg("\"PORT\" is invalid", *argv);
1195 sel->dport = htons(sel->dport);
1196 if (sel->dport)
1197 sel->dport_mask = ~((__u16)0);
1198
1199 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1200
c70b36d2 1201 } else if (strcmp(*argv, "type") == 0) {
1202 typep = *argv;
1203
1204 NEXT_ARG();
1205
1206 if (get_u16(&sel->sport, *argv, 0) ||
1207 (sel->sport & ~((__u16)0xff)))
1208 invarg("\"type\" value is invalid", *argv);
1209 sel->sport = htons(sel->sport);
1210 sel->sport_mask = ~((__u16)0);
1211
1212 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1213
1214
1215 } else if (strcmp(*argv, "code") == 0) {
1216 codep = *argv;
1217
1218 NEXT_ARG();
1219
1220 if (get_u16(&sel->dport, *argv, 0) ||
1221 (sel->dport & ~((__u16)0xff)))
1222 invarg("\"code\" value is invalid", *argv);
1223 sel->dport = htons(sel->dport);
1224 sel->dport_mask = ~((__u16)0);
1225
1226 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1227
4a9608e6
TT
1228 } else if (strcmp(*argv, "key") == 0) {
1229 unsigned uval;
1230
1231 grekey = *argv;
1232
1233 NEXT_ARG();
1234
1235 if (strchr(*argv, '.'))
1236 uval = htonl(get_addr32(*argv));
1237 else {
1238 if (get_unsigned(&uval, *argv, 0)<0) {
1239 fprintf(stderr, "invalid value of \"key\"\n");
1240 exit(-1);
1241 }
1242 }
1243
1244 sel->sport = htons(uval >> 16);
1245 sel->dport = htons(uval & 0xffff);
1246 sel->sport_mask = ~((__u16)0);
1247 sel->dport_mask = ~((__u16)0);
1248
1249 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1250
c7699875 1251 } else {
1252 PREV_ARG(); /* back track */
1253 break;
1254 }
1255
1256 if (!NEXT_ARG_OK())
1257 break;
1258 NEXT_ARG();
1259 }
1260 if (argc == *argcp)
1261 missarg("UPSPEC");
c70b36d2 1262 if (sportp || dportp) {
1263 switch (sel->proto) {
1264 case IPPROTO_TCP:
1265 case IPPROTO_UDP:
1266 case IPPROTO_SCTP:
27356a5e 1267 case IPPROTO_DCCP:
c70b36d2 1268 break;
1269 default:
1270 fprintf(stderr, "\"sport\" and \"dport\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1271 exit(1);
1272 }
1273 }
1274 if (typep || codep) {
1275 switch (sel->proto) {
1276 case IPPROTO_ICMP:
1277 case IPPROTO_ICMPV6:
0bf0fbc4 1278 case IPPROTO_MH:
c70b36d2 1279 break;
1280 default:
1281 fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
1282 exit(1);
1283 }
1284 }
4a9608e6
TT
1285 if (grekey) {
1286 switch (sel->proto) {
1287 case IPPROTO_GRE:
1288 break;
1289 default:
1290 fprintf(stderr, "\"key\" is invalid with proto=%s\n", strxf_proto(sel->proto));
1291 exit(1);
1292 }
1293 }
c7699875 1294
1295 *argcp = argc;
1296 *argvp = argv;
1297
1298 return 0;
1299}
1300
1301int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1302{
1303 int argc = *argcp;
1304 char **argv = *argvp;
1305 inet_prefix dst;
1306 inet_prefix src;
7809c616 1307 char *upspecp = NULL;
c7699875 1308
1309 memset(&dst, 0, sizeof(dst));
1310 memset(&src, 0, sizeof(src));
1311
1312 while (1) {
1313 if (strcmp(*argv, "src") == 0) {
1314 NEXT_ARG();
1315
1316 get_prefix(&src, *argv, preferred_family);
1317 if (src.family == AF_UNSPEC)
eaa34ee3 1318 invarg("\"src\" address family is AF_UNSPEC", *argv);
c7699875 1319 sel->family = src.family;
1320
1321 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1322 sel->prefixlen_s = src.bitlen;
1323
1324 filter.sel_src_mask = src.bitlen;
1325
1326 } else if (strcmp(*argv, "dst") == 0) {
1327 NEXT_ARG();
1328
1329 get_prefix(&dst, *argv, preferred_family);
1330 if (dst.family == AF_UNSPEC)
eaa34ee3 1331 invarg("\"dst\" address family is AF_UNSPEC", *argv);
c7699875 1332 sel->family = dst.family;
1333
1334 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1335 sel->prefixlen_d = dst.bitlen;
1336
1337 filter.sel_dst_mask = dst.bitlen;
1338
c7699875 1339 } else if (strcmp(*argv, "dev") == 0) {
1340 int ifindex;
1341
1342 NEXT_ARG();
1343
1344 if (strcmp(*argv, "none") == 0)
1345 ifindex = 0;
1346 else {
dcb283c3 1347 ifindex = ll_name_to_index(*argv);
c7699875 1348 if (ifindex <= 0)
1349 invarg("\"DEV\" is invalid", *argv);
1350 }
1351 sel->ifindex = ifindex;
1352
1353 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1354
1355 } else {
7809c616 1356 if (upspecp) {
1357 PREV_ARG(); /* back track */
1358 break;
1359 } else {
1360 upspecp = *argv;
1361 xfrm_selector_upspec_parse(sel, &argc, &argv);
1362 }
c7699875 1363 }
1364
1365 if (!NEXT_ARG_OK())
1366 break;
1367
1368 NEXT_ARG();
1369 }
1370
1371 if (src.family && dst.family && (src.family != dst.family))
eaa34ee3 1372 invarg("the same address family is required between \"src\" and \"dst\"", *argv);
c7699875 1373
1374 if (argc == *argcp)
1375 missarg("SELECTOR");
1376
1377 *argcp = argc;
1378 *argvp = argv;
1379
1380 return 0;
1381}
1382
1383int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1384 int *argcp, char ***argvp)
1385{
1386 int argc = *argcp;
1387 char **argv = *argvp;
1388 int ret;
1389
1390 if (strcmp(*argv, "time-soft") == 0) {
1391 NEXT_ARG();
1392 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1393 if (ret)
1394 invarg("\"time-soft\" value is invalid", *argv);
1395 } else if (strcmp(*argv, "time-hard") == 0) {
1396 NEXT_ARG();
1397 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1398 if (ret)
1399 invarg("\"time-hard\" value is invalid", *argv);
1400 } else if (strcmp(*argv, "time-use-soft") == 0) {
1401 NEXT_ARG();
1402 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1403 if (ret)
1404 invarg("\"time-use-soft\" value is invalid", *argv);
1405 } else if (strcmp(*argv, "time-use-hard") == 0) {
1406 NEXT_ARG();
1407 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1408 if (ret)
1409 invarg("\"time-use-hard\" value is invalid", *argv);
1410 } else if (strcmp(*argv, "byte-soft") == 0) {
1411 NEXT_ARG();
1412 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1413 if (ret)
1414 invarg("\"byte-soft\" value is invalid", *argv);
1415 } else if (strcmp(*argv, "byte-hard") == 0) {
1416 NEXT_ARG();
1417 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1418 if (ret)
1419 invarg("\"byte-hard\" value is invalid", *argv);
1420 } else if (strcmp(*argv, "packet-soft") == 0) {
1421 NEXT_ARG();
1422 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1423 if (ret)
1424 invarg("\"packet-soft\" value is invalid", *argv);
1425 } else if (strcmp(*argv, "packet-hard") == 0) {
1426 NEXT_ARG();
1427 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1428 if (ret)
1429 invarg("\"packet-hard\" value is invalid", *argv);
1430 } else
1431 invarg("\"LIMIT\" is invalid", *argv);
1432
1433 *argcp = argc;
1434 *argvp = argv;
1435
1436 return 0;
1437}
1438
1439int do_xfrm(int argc, char **argv)
1440{
1441 memset(&filter, 0, sizeof(filter));
1442
1443 if (argc < 1)
1444 usage();
1445
ad273962 1446 if (matches(*argv, "state") == 0 ||
15ac4cdc 1447 matches(*argv, "sa") == 0)
c7699875 1448 return do_xfrm_state(argc-1, argv+1);
15ac4cdc 1449 else if (matches(*argv, "policy") == 0)
c7699875 1450 return do_xfrm_policy(argc-1, argv+1);
15ac4cdc 1451 else if (matches(*argv, "monitor") == 0)
1452 return do_xfrm_monitor(argc-1, argv+1);
ad273962 1453 else if (matches(*argv, "help") == 0) {
c7699875 1454 usage();
1455 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);
1456 exit(-1);
1457 }
1458 usage();
1459}