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