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