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