]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipxfrm.c
95f91a537759f90a07832537c92969d46d8a2921
[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,
292 saddr, abuf, sizeof(abuf)));
293 memset(abuf, '\0', sizeof(abuf));
294 fprintf(fp, "dst %s", rt_addr_n2a(family,
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 ", rt_addr_n2a(f, &sel->saddr, abuf, sizeof(abuf)),
459 sel->prefixlen_s);
460
461 memset(abuf, '\0', sizeof(abuf));
462 fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, &sel->daddr, abuf, sizeof(abuf)),
463 sel->prefixlen_d);
464
465 if (sel->proto)
466 fprintf(fp, "proto %s ", strxf_proto(sel->proto));
467 switch (sel->proto) {
468 case IPPROTO_TCP:
469 case IPPROTO_UDP:
470 case IPPROTO_SCTP:
471 case IPPROTO_DCCP:
472 default: /* XXX */
473 if (sel->sport_mask)
474 fprintf(fp, "sport %u ", ntohs(sel->sport));
475 if (sel->dport_mask)
476 fprintf(fp, "dport %u ", ntohs(sel->dport));
477 break;
478 case IPPROTO_ICMP:
479 case IPPROTO_ICMPV6:
480 /* type/code is stored at sport/dport in selector */
481 if (sel->sport_mask)
482 fprintf(fp, "type %u ", ntohs(sel->sport));
483 if (sel->dport_mask)
484 fprintf(fp, "code %u ", ntohs(sel->dport));
485 break;
486 case IPPROTO_GRE:
487 if (sel->sport_mask || sel->dport_mask)
488 fprintf(fp, "key %u ",
489 (((__u32)ntohs(sel->sport)) << 16) +
490 ntohs(sel->dport));
491 break;
492 case IPPROTO_MH:
493 if (sel->sport_mask)
494 fprintf(fp, "type %u ", ntohs(sel->sport));
495 if (sel->dport_mask) {
496 if (show_stats > 0)
497 fprintf(fp, "(dport) 0x%.4x ", sel->dport);
498 }
499 break;
500 }
501
502 if (sel->ifindex > 0)
503 fprintf(fp, "dev %s ", ll_index_to_name(sel->ifindex));
504
505 if (show_stats > 0)
506 fprintf(fp, "uid %u", sel->user);
507
508 fprintf(fp, "%s", _SL_);
509 }
510
511 static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
512 FILE *fp, const char *prefix, int newline)
513 {
514 int keylen;
515 int i;
516
517 if (prefix)
518 fputs(prefix, fp);
519
520 fprintf(fp, "%s ", strxf_algotype(type));
521
522 if (len < sizeof(*algo)) {
523 fprintf(fp, "(ERROR truncated)");
524 goto fin;
525 }
526 len -= sizeof(*algo);
527
528 fprintf(fp, "%s ", algo->alg_name);
529
530 keylen = algo->alg_key_len / 8;
531 if (len < keylen) {
532 fprintf(fp, "(ERROR truncated)");
533 goto fin;
534 }
535
536 if (keylen > 0) {
537 fprintf(fp, "0x");
538 for (i = 0; i < keylen; i ++)
539 fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
540
541 if (show_stats > 0)
542 fprintf(fp, " (%d bits)", algo->alg_key_len);
543 }
544
545 fin:
546 if (newline)
547 fprintf(fp, "%s", _SL_);
548 }
549
550 static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
551 FILE *fp, const char *prefix)
552 {
553 return __xfrm_algo_print(algo, type, len, fp, prefix, 1);
554 }
555
556 static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
557 FILE *fp, const char *prefix)
558 {
559 struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
560
561 memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
562 base_algo->alg_key_len = algo->alg_key_len;
563 memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
564
565 __xfrm_algo_print(base_algo, XFRMA_ALG_AEAD, len, fp, prefix, 0);
566
567 fprintf(fp, " %d", algo->alg_icv_len);
568
569 fprintf(fp, "%s", _SL_);
570 }
571
572 static void xfrm_auth_trunc_print(struct xfrm_algo_auth *algo, int len,
573 FILE *fp, const char *prefix)
574 {
575 struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
576
577 memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
578 base_algo->alg_key_len = algo->alg_key_len;
579 memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
580
581 __xfrm_algo_print(base_algo, XFRMA_ALG_AUTH_TRUNC, len, fp, prefix, 0);
582
583 fprintf(fp, " %d", algo->alg_trunc_len);
584
585 fprintf(fp, "%s", _SL_);
586 }
587
588 static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
589 FILE *fp, const char *prefix)
590 {
591 int ntmpls = len / sizeof(struct xfrm_user_tmpl);
592 int i;
593
594 if (ntmpls <= 0) {
595 if (prefix)
596 fputs(prefix, fp);
597 fprintf(fp, "(ERROR \"tmpl\" truncated)");
598 fprintf(fp, "%s", _SL_);
599 return;
600 }
601
602 for (i = 0; i < ntmpls; i++) {
603 struct xfrm_user_tmpl *tmpl = &tmpls[i];
604
605 if (prefix)
606 fputs(prefix, fp);
607
608 xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
609 tmpl->reqid, tmpl->family, 0, fp, prefix, "tmpl ");
610
611 if (show_stats > 0 || tmpl->optional) {
612 if (prefix)
613 fputs(prefix, fp);
614 fprintf(fp, "\t");
615 switch (tmpl->optional) {
616 case 0:
617 if (show_stats > 0)
618 fprintf(fp, "level required ");
619 break;
620 case 1:
621 fprintf(fp, "level use ");
622 break;
623 default:
624 fprintf(fp, "level %u ", tmpl->optional);
625 break;
626 }
627
628 if (show_stats > 0)
629 fprintf(fp, "share %s ", strxf_share(tmpl->share));
630
631 fprintf(fp, "%s", _SL_);
632 }
633
634 if (show_stats > 0) {
635 if (prefix)
636 fputs(prefix, fp);
637 fprintf(fp, "\t");
638 fprintf(fp, "%s-mask %s ",
639 strxf_algotype(XFRMA_ALG_CRYPT),
640 strxf_mask32(tmpl->ealgos));
641 fprintf(fp, "%s-mask %s ",
642 strxf_algotype(XFRMA_ALG_AUTH),
643 strxf_mask32(tmpl->aalgos));
644 fprintf(fp, "%s-mask %s",
645 strxf_algotype(XFRMA_ALG_COMP),
646 strxf_mask32(tmpl->calgos));
647
648 fprintf(fp, "%s", _SL_);
649 }
650 }
651 }
652
653 int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp)
654 {
655 int argc = *argcp;
656 char **argv = *argvp;
657
658 NEXT_ARG();
659 if (get_u32(&mark->v, *argv, 0)) {
660 invarg("MARK value is invalid\n", *argv);
661 }
662 if (argc > 1)
663 NEXT_ARG();
664 else { /* last entry on parse line */
665 mark->m = 0xffffffff;
666 goto done;
667 }
668
669 if (strcmp(*argv, "mask") == 0) {
670 NEXT_ARG();
671 if (get_u32(&mark->m, *argv, 0)) {
672 invarg("MASK value is invalid\n", *argv);
673 }
674 } else {
675 mark->m = 0xffffffff;
676 PREV_ARG();
677 }
678
679 done:
680 *argcp = argc;
681 *argvp = argv;
682
683 return 0;
684 }
685
686 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
687 FILE *fp, const char *prefix)
688 {
689 if (tb[XFRMA_MARK]) {
690 struct rtattr *rta = tb[XFRMA_MARK];
691 struct xfrm_mark *m = (struct xfrm_mark *) RTA_DATA(rta);
692 fprintf(fp, "\tmark %#x/%#x", m->v, m->m);
693 fprintf(fp, "%s", _SL_);
694 }
695
696 if (tb[XFRMA_ALG_AUTH] && !tb[XFRMA_ALG_AUTH_TRUNC]) {
697 struct rtattr *rta = tb[XFRMA_ALG_AUTH];
698 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
699 XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
700 }
701
702 if (tb[XFRMA_ALG_AUTH_TRUNC]) {
703 struct rtattr *rta = tb[XFRMA_ALG_AUTH_TRUNC];
704 xfrm_auth_trunc_print((struct xfrm_algo_auth *) RTA_DATA(rta),
705 RTA_PAYLOAD(rta), fp, prefix);
706 }
707
708 if (tb[XFRMA_ALG_AEAD]) {
709 struct rtattr *rta = tb[XFRMA_ALG_AEAD];
710 xfrm_aead_print((struct xfrm_algo_aead *)RTA_DATA(rta),
711 RTA_PAYLOAD(rta), fp, prefix);
712 }
713
714 if (tb[XFRMA_ALG_CRYPT]) {
715 struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
716 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
717 XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix);
718 }
719
720 if (tb[XFRMA_ALG_COMP]) {
721 struct rtattr *rta = tb[XFRMA_ALG_COMP];
722 xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
723 XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix);
724 }
725
726 if (tb[XFRMA_ENCAP]) {
727 struct xfrm_encap_tmpl *e;
728 char abuf[256];
729
730 if (prefix)
731 fputs(prefix, fp);
732 fprintf(fp, "encap ");
733
734 if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
735 fprintf(fp, "(ERROR truncated)");
736 fprintf(fp, "%s", _SL_);
737 return;
738 }
739 e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
740
741 fprintf(fp, "type ");
742 switch (e->encap_type) {
743 case 1:
744 fprintf(fp, "espinudp-nonike ");
745 break;
746 case 2:
747 fprintf(fp, "espinudp ");
748 break;
749 default:
750 fprintf(fp, "%u ", e->encap_type);
751 break;
752 }
753 fprintf(fp, "sport %u ", ntohs(e->encap_sport));
754 fprintf(fp, "dport %u ", ntohs(e->encap_dport));
755
756 memset(abuf, '\0', sizeof(abuf));
757 fprintf(fp, "addr %s",
758 rt_addr_n2a(family, &e->encap_oa, abuf, sizeof(abuf)));
759 fprintf(fp, "%s", _SL_);
760 }
761
762 if (tb[XFRMA_TMPL]) {
763 struct rtattr *rta = tb[XFRMA_TMPL];
764 xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta),
765 RTA_PAYLOAD(rta), fp, prefix);
766 }
767
768 if (tb[XFRMA_COADDR]) {
769 char abuf[256];
770 xfrm_address_t *coa;
771
772 if (prefix)
773 fputs(prefix, fp);
774 fprintf(fp, "coa ");
775
776 coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]);
777
778 if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
779 fprintf(fp, "(ERROR truncated)");
780 fprintf(fp, "%s", _SL_);
781 return;
782 }
783
784 memset(abuf, '\0', sizeof(abuf));
785 fprintf(fp, "%s",
786 rt_addr_n2a(family, coa,
787 abuf, sizeof(abuf)));
788 fprintf(fp, "%s", _SL_);
789 }
790
791 if (tb[XFRMA_LASTUSED]) {
792 __u64 lastused;
793
794 if (prefix)
795 fputs(prefix, fp);
796 fprintf(fp, "lastused ");
797
798 if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
799 fprintf(fp, "(ERROR truncated)");
800 fprintf(fp, "%s", _SL_);
801 return;
802 }
803
804 lastused = rta_getattr_u64(tb[XFRMA_LASTUSED]);
805
806 fprintf(fp, "%s", strxf_time(lastused));
807 fprintf(fp, "%s", _SL_);
808 }
809
810 if (tb[XFRMA_REPLAY_VAL]) {
811 struct xfrm_replay_state *replay;
812
813 if (prefix)
814 fputs(prefix, fp);
815 fprintf(fp, "anti-replay context: ");
816
817 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_VAL]) < sizeof(*replay)) {
818 fprintf(fp, "(ERROR truncated)");
819 fprintf(fp, "%s", _SL_);
820 return;
821 }
822
823 replay = (struct xfrm_replay_state *)RTA_DATA(tb[XFRMA_REPLAY_VAL]);
824 fprintf(fp, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
825 replay->seq, replay->oseq, replay->bitmap);
826 fprintf(fp, "%s", _SL_);
827 }
828
829 if (tb[XFRMA_REPLAY_ESN_VAL]) {
830 struct xfrm_replay_state_esn *replay;
831 unsigned int i, j;
832
833 if (prefix)
834 fputs(prefix, fp);
835 fprintf(fp, "anti-replay esn context:");
836
837 if (RTA_PAYLOAD(tb[XFRMA_REPLAY_ESN_VAL]) < sizeof(*replay)) {
838 fprintf(fp, "(ERROR truncated)");
839 fprintf(fp, "%s", _SL_);
840 return;
841 }
842 fprintf(fp, "%s", _SL_);
843
844 replay = (struct xfrm_replay_state_esn *)RTA_DATA(tb[XFRMA_REPLAY_ESN_VAL]);
845 if (prefix)
846 fputs(prefix, fp);
847 fprintf(fp, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
848 replay->seq_hi, replay->seq, replay->oseq_hi,
849 replay->oseq);
850 fprintf(fp, "%s", _SL_);
851 if (prefix)
852 fputs(prefix, fp);
853 fprintf(fp, " replay_window %u, bitmap-length %u",
854 replay->replay_window, replay->bmp_len);
855 for (i = replay->bmp_len, j = 0; i; i--) {
856 if (j++ % 8 == 0) {
857 fprintf(fp, "%s", _SL_);
858 if (prefix)
859 fputs(prefix, fp);
860 fprintf(fp, " ");
861 }
862 fprintf(fp, "%08x ", replay->bmp[i - 1]);
863 }
864 fprintf(fp, "%s", _SL_);
865 }
866 }
867
868 static int xfrm_selector_iszero(struct xfrm_selector *s)
869 {
870 struct xfrm_selector s0;
871
872 memset(&s0, 0, sizeof(s0));
873
874 return (memcmp(&s0, s, sizeof(s0)) == 0);
875 }
876
877 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
878 struct rtattr *tb[], FILE *fp, const char *prefix,
879 const char *title)
880 {
881 char buf[STRBUF_SIZE];
882 int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
883
884 memset(buf, '\0', sizeof(buf));
885
886 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
887 xsinfo->reqid, xsinfo->family, force_spi, fp,
888 prefix, title);
889
890 if (prefix)
891 STRBUF_CAT(buf, prefix);
892 STRBUF_CAT(buf, "\t");
893
894 fputs(buf, fp);
895 fprintf(fp, "replay-window %u ", xsinfo->replay_window);
896 if (show_stats > 0)
897 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
898 if (show_stats > 0 || xsinfo->flags) {
899 __u8 flags = xsinfo->flags;
900
901 fprintf(fp, "flag ");
902 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn");
903 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
904 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
905 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
906 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp");
907 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec");
908 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ALIGN4, "align4");
909 XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ESN, "esn");
910 if (flags)
911 fprintf(fp, "%x", flags);
912 }
913 if (show_stats > 0 && tb[XFRMA_SA_EXTRA_FLAGS]) {
914 __u32 extra_flags = *(__u32 *)RTA_DATA(tb[XFRMA_SA_EXTRA_FLAGS]);
915
916 fprintf(fp, "extra_flag ");
917 XFRM_FLAG_PRINT(fp, extra_flags,
918 XFRM_SA_XFLAG_DONT_ENCAP_DSCP,
919 "dont-encap-dscp");
920 if (extra_flags)
921 fprintf(fp, "%x", extra_flags);
922 }
923 if (show_stats > 0)
924 fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
925 fprintf(fp, "%s", _SL_);
926
927 xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
928
929 if (!xfrm_selector_iszero(&xsinfo->sel)) {
930 char sbuf[STRBUF_SIZE];
931
932 memcpy(sbuf, buf, sizeof(sbuf));
933 STRBUF_CAT(sbuf, "sel ");
934
935 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
936 }
937
938 if (show_stats > 0) {
939 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
940 xfrm_stats_print(&xsinfo->stats, fp, buf);
941 }
942
943 if (tb[XFRMA_SEC_CTX]) {
944 struct xfrm_user_sec_ctx *sctx;
945
946 fprintf(fp, "\tsecurity context ");
947
948 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
949 fprintf(fp, "(ERROR truncated)");
950
951 sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
952
953 fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_);
954 }
955
956 }
957
958 void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
959 struct rtattr *tb[], FILE *fp, const char *prefix,
960 const char *title)
961 {
962 char buf[STRBUF_SIZE];
963
964 memset(buf, '\0', sizeof(buf));
965
966 xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
967
968 if (tb[XFRMA_SEC_CTX]) {
969 struct xfrm_user_sec_ctx *sctx;
970
971 fprintf(fp, "\tsecurity context ");
972
973 if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
974 fprintf(fp, "(ERROR truncated)");
975
976 sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
977
978 fprintf(fp, "%s ", (char *)(sctx + 1));
979 fprintf(fp, "%s", _SL_);
980 }
981
982 if (prefix)
983 STRBUF_CAT(buf, prefix);
984 STRBUF_CAT(buf, "\t");
985
986 fputs(buf, fp);
987 if (xpinfo->dir >= XFRM_POLICY_MAX) {
988 xpinfo->dir -= XFRM_POLICY_MAX;
989 fprintf(fp, "socket ");
990 } else
991 fprintf(fp, "dir ");
992
993 switch (xpinfo->dir) {
994 case XFRM_POLICY_IN:
995 fprintf(fp, "in");
996 break;
997 case XFRM_POLICY_OUT:
998 fprintf(fp, "out");
999 break;
1000 case XFRM_POLICY_FWD:
1001 fprintf(fp, "fwd");
1002 break;
1003 default:
1004 fprintf(fp, "%u", xpinfo->dir);
1005 break;
1006 }
1007 fprintf(fp, " ");
1008
1009 switch (xpinfo->action) {
1010 case XFRM_POLICY_ALLOW:
1011 if (show_stats > 0)
1012 fprintf(fp, "action allow ");
1013 break;
1014 case XFRM_POLICY_BLOCK:
1015 fprintf(fp, "action block ");
1016 break;
1017 default:
1018 fprintf(fp, "action %u ", xpinfo->action);
1019 break;
1020 }
1021
1022 if (show_stats)
1023 fprintf(fp, "index %u ", xpinfo->index);
1024 fprintf(fp, "priority %u ", xpinfo->priority);
1025
1026 if (tb[XFRMA_POLICY_TYPE]) {
1027 struct xfrm_userpolicy_type *upt;
1028
1029 fprintf(fp, "ptype ");
1030
1031 if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
1032 fprintf(fp, "(ERROR truncated)");
1033
1034 upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
1035 fprintf(fp, "%s ", strxf_ptype(upt->type));
1036 }
1037
1038 if (show_stats > 0)
1039 fprintf(fp, "share %s ", strxf_share(xpinfo->share));
1040
1041 if (show_stats > 0 || xpinfo->flags) {
1042 __u8 flags = xpinfo->flags;
1043
1044 fprintf(fp, "flag ");
1045 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
1046 XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_ICMP, "icmp");
1047 if (flags)
1048 fprintf(fp, "%x", flags);
1049 }
1050 if (show_stats > 0)
1051 fprintf(fp, " (0x%s)", strxf_mask8(xpinfo->flags));
1052 fprintf(fp, "%s", _SL_);
1053
1054 if (show_stats > 0)
1055 xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
1056
1057 xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
1058 }
1059
1060 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
1061 int loose, int *argcp, char ***argvp)
1062 {
1063 int argc = *argcp;
1064 char **argv = *argvp;
1065 inet_prefix dst;
1066 inet_prefix src;
1067
1068 memset(&dst, 0, sizeof(dst));
1069 memset(&src, 0, sizeof(src));
1070
1071 while (1) {
1072 if (strcmp(*argv, "src") == 0) {
1073 NEXT_ARG();
1074
1075 get_prefix(&src, *argv, preferred_family);
1076 if (src.family == AF_UNSPEC)
1077 invarg("value after \"src\" has an unrecognized address family", *argv);
1078 if (family)
1079 *family = src.family;
1080
1081 memcpy(saddr, &src.data, sizeof(*saddr));
1082
1083 filter.id_src_mask = src.bitlen;
1084
1085 } else if (strcmp(*argv, "dst") == 0) {
1086 NEXT_ARG();
1087
1088 get_prefix(&dst, *argv, preferred_family);
1089 if (dst.family == AF_UNSPEC)
1090 invarg("value after \"dst\" has an unrecognized address family", *argv);
1091 if (family)
1092 *family = dst.family;
1093
1094 memcpy(&id->daddr, &dst.data, sizeof(id->daddr));
1095
1096 filter.id_dst_mask = dst.bitlen;
1097
1098 } else if (strcmp(*argv, "proto") == 0) {
1099 int ret;
1100
1101 NEXT_ARG();
1102
1103 ret = xfrm_xfrmproto_getbyname(*argv);
1104 if (ret < 0)
1105 invarg("XFRM-PROTO value is invalid", *argv);
1106
1107 id->proto = (__u8)ret;
1108
1109 filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
1110
1111 } else if (strcmp(*argv, "spi") == 0) {
1112 __u32 spi;
1113
1114 NEXT_ARG();
1115 if (get_u32(&spi, *argv, 0))
1116 invarg("SPI value is invalid", *argv);
1117
1118 spi = htonl(spi);
1119 id->spi = spi;
1120
1121 filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
1122
1123 } else {
1124 PREV_ARG(); /* back track */
1125 break;
1126 }
1127
1128 if (!NEXT_ARG_OK())
1129 break;
1130 NEXT_ARG();
1131 }
1132
1133 if (src.family && dst.family && (src.family != dst.family))
1134 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
1135
1136 if (id->spi && id->proto) {
1137 if (xfrm_xfrmproto_is_ro(id->proto)) {
1138 fprintf(stderr, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
1139 strxf_xfrmproto(id->proto));
1140 exit(1);
1141 } else if (id->proto == IPPROTO_COMP && ntohl(id->spi) >= 0x10000) {
1142 fprintf(stderr, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
1143 strxf_xfrmproto(id->proto));
1144 exit(1);
1145 }
1146 }
1147
1148 if (loose == 0 && id->proto == 0)
1149 missarg("XFRM-PROTO");
1150 if (argc == *argcp)
1151 missarg("ID");
1152
1153 *argcp = argc;
1154 *argvp = argv;
1155
1156 return 0;
1157 }
1158
1159 int xfrm_mode_parse(__u8 *mode, int *argcp, char ***argvp)
1160 {
1161 int argc = *argcp;
1162 char **argv = *argvp;
1163
1164 if (matches(*argv, "transport") == 0)
1165 *mode = XFRM_MODE_TRANSPORT;
1166 else if (matches(*argv, "tunnel") == 0)
1167 *mode = XFRM_MODE_TUNNEL;
1168 else if (matches(*argv, "ro") == 0)
1169 *mode = XFRM_MODE_ROUTEOPTIMIZATION;
1170 else if (matches(*argv, "in_trigger") == 0)
1171 *mode = XFRM_MODE_IN_TRIGGER;
1172 else if (matches(*argv, "beet") == 0)
1173 *mode = XFRM_MODE_BEET;
1174 else
1175 invarg("MODE value is invalid", *argv);
1176
1177 *argcp = argc;
1178 *argvp = argv;
1179
1180 return 0;
1181 }
1182
1183 int xfrm_encap_type_parse(__u16 *type, int *argcp, char ***argvp)
1184 {
1185 int argc = *argcp;
1186 char **argv = *argvp;
1187
1188 if (strcmp(*argv, "espinudp-nonike") == 0)
1189 *type = 1;
1190 else if (strcmp(*argv, "espinudp") == 0)
1191 *type = 2;
1192 else
1193 invarg("ENCAP-TYPE value is invalid", *argv);
1194
1195 *argcp = argc;
1196 *argvp = argv;
1197
1198 return 0;
1199 }
1200
1201 /* NOTE: reqid is used by host-byte order */
1202 int xfrm_reqid_parse(__u32 *reqid, int *argcp, char ***argvp)
1203 {
1204 int argc = *argcp;
1205 char **argv = *argvp;
1206
1207 if (get_u32(reqid, *argv, 0))
1208 invarg("REQID value is invalid", *argv);
1209
1210 *argcp = argc;
1211 *argvp = argv;
1212
1213 return 0;
1214 }
1215
1216 static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
1217 int *argcp, char ***argvp)
1218 {
1219 int argc = *argcp;
1220 char **argv = *argvp;
1221 char *sportp = NULL;
1222 char *dportp = NULL;
1223 char *typep = NULL;
1224 char *codep = NULL;
1225 char *grekey = NULL;
1226
1227 while (1) {
1228 if (strcmp(*argv, "proto") == 0) {
1229 __u8 upspec;
1230
1231 NEXT_ARG();
1232
1233 if (strcmp(*argv, "any") == 0)
1234 upspec = 0;
1235 else {
1236 struct protoent *pp;
1237 pp = getprotobyname(*argv);
1238 if (pp)
1239 upspec = pp->p_proto;
1240 else {
1241 if (get_u8(&upspec, *argv, 0))
1242 invarg("PROTO value is invalid", *argv);
1243 }
1244 }
1245 sel->proto = upspec;
1246
1247 filter.upspec_proto_mask = XFRM_FILTER_MASK_FULL;
1248
1249 } else if (strcmp(*argv, "sport") == 0) {
1250 sportp = *argv;
1251
1252 NEXT_ARG();
1253
1254 if (get_u16(&sel->sport, *argv, 0))
1255 invarg("value after \"sport\" is invalid", *argv);
1256 sel->sport = htons(sel->sport);
1257 if (sel->sport)
1258 sel->sport_mask = ~((__u16)0);
1259
1260 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1261
1262 } else if (strcmp(*argv, "dport") == 0) {
1263 dportp = *argv;
1264
1265 NEXT_ARG();
1266
1267 if (get_u16(&sel->dport, *argv, 0))
1268 invarg("value after \"dport\" is invalid", *argv);
1269 sel->dport = htons(sel->dport);
1270 if (sel->dport)
1271 sel->dport_mask = ~((__u16)0);
1272
1273 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1274
1275 } else if (strcmp(*argv, "type") == 0) {
1276 typep = *argv;
1277
1278 NEXT_ARG();
1279
1280 if (get_u16(&sel->sport, *argv, 0) ||
1281 (sel->sport & ~((__u16)0xff)))
1282 invarg("value after \"type\" is invalid", *argv);
1283 sel->sport = htons(sel->sport);
1284 sel->sport_mask = ~((__u16)0);
1285
1286 filter.upspec_sport_mask = XFRM_FILTER_MASK_FULL;
1287
1288
1289 } else if (strcmp(*argv, "code") == 0) {
1290 codep = *argv;
1291
1292 NEXT_ARG();
1293
1294 if (get_u16(&sel->dport, *argv, 0) ||
1295 (sel->dport & ~((__u16)0xff)))
1296 invarg("value after \"code\" is invalid", *argv);
1297 sel->dport = htons(sel->dport);
1298 sel->dport_mask = ~((__u16)0);
1299
1300 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1301
1302 } else if (strcmp(*argv, "key") == 0) {
1303 unsigned uval;
1304
1305 grekey = *argv;
1306
1307 NEXT_ARG();
1308
1309 if (strchr(*argv, '.'))
1310 uval = htonl(get_addr32(*argv));
1311 else {
1312 if (get_unsigned(&uval, *argv, 0)<0) {
1313 fprintf(stderr, "value after \"key\" is invalid\n");
1314 exit(-1);
1315 }
1316 }
1317
1318 sel->sport = htons(uval >> 16);
1319 sel->dport = htons(uval & 0xffff);
1320 sel->sport_mask = ~((__u16)0);
1321 sel->dport_mask = ~((__u16)0);
1322
1323 filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
1324
1325 } else {
1326 PREV_ARG(); /* back track */
1327 break;
1328 }
1329
1330 if (!NEXT_ARG_OK())
1331 break;
1332 NEXT_ARG();
1333 }
1334 if (argc == *argcp)
1335 missarg("UPSPEC");
1336 if (sportp || dportp) {
1337 switch (sel->proto) {
1338 case IPPROTO_TCP:
1339 case IPPROTO_UDP:
1340 case IPPROTO_SCTP:
1341 case IPPROTO_DCCP:
1342 break;
1343 default:
1344 fprintf(stderr, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
1345 exit(1);
1346 }
1347 }
1348 if (typep || codep) {
1349 switch (sel->proto) {
1350 case IPPROTO_ICMP:
1351 case IPPROTO_ICMPV6:
1352 case IPPROTO_MH:
1353 break;
1354 default:
1355 fprintf(stderr, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
1356 exit(1);
1357 }
1358 }
1359 if (grekey) {
1360 switch (sel->proto) {
1361 case IPPROTO_GRE:
1362 break;
1363 default:
1364 fprintf(stderr, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
1365 exit(1);
1366 }
1367 }
1368
1369 *argcp = argc;
1370 *argvp = argv;
1371
1372 return 0;
1373 }
1374
1375 int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
1376 {
1377 int argc = *argcp;
1378 char **argv = *argvp;
1379 inet_prefix dst;
1380 inet_prefix src;
1381 char *upspecp = NULL;
1382
1383 memset(&dst, 0, sizeof(dst));
1384 memset(&src, 0, sizeof(src));
1385
1386 while (1) {
1387 if (strcmp(*argv, "src") == 0) {
1388 NEXT_ARG();
1389
1390 get_prefix(&src, *argv, preferred_family);
1391 if (src.family == AF_UNSPEC)
1392 invarg("value after \"src\" has an unrecognized address family", *argv);
1393 sel->family = src.family;
1394
1395 memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
1396 sel->prefixlen_s = src.bitlen;
1397
1398 filter.sel_src_mask = src.bitlen;
1399
1400 } else if (strcmp(*argv, "dst") == 0) {
1401 NEXT_ARG();
1402
1403 get_prefix(&dst, *argv, preferred_family);
1404 if (dst.family == AF_UNSPEC)
1405 invarg("value after \"dst\" has an unrecognized address family", *argv);
1406 sel->family = dst.family;
1407
1408 memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
1409 sel->prefixlen_d = dst.bitlen;
1410
1411 filter.sel_dst_mask = dst.bitlen;
1412
1413 } else if (strcmp(*argv, "dev") == 0) {
1414 int ifindex;
1415
1416 NEXT_ARG();
1417
1418 if (strcmp(*argv, "none") == 0)
1419 ifindex = 0;
1420 else {
1421 ifindex = ll_name_to_index(*argv);
1422 if (ifindex <= 0)
1423 invarg("DEV value is invalid", *argv);
1424 }
1425 sel->ifindex = ifindex;
1426
1427 filter.sel_dev_mask = XFRM_FILTER_MASK_FULL;
1428
1429 } else {
1430 if (upspecp) {
1431 PREV_ARG(); /* back track */
1432 break;
1433 } else {
1434 upspecp = *argv;
1435 xfrm_selector_upspec_parse(sel, &argc, &argv);
1436 }
1437 }
1438
1439 if (!NEXT_ARG_OK())
1440 break;
1441
1442 NEXT_ARG();
1443 }
1444
1445 if (src.family && dst.family && (src.family != dst.family))
1446 invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
1447
1448 if (argc == *argcp)
1449 missarg("SELECTOR");
1450
1451 *argcp = argc;
1452 *argvp = argv;
1453
1454 return 0;
1455 }
1456
1457 int xfrm_lifetime_cfg_parse(struct xfrm_lifetime_cfg *lft,
1458 int *argcp, char ***argvp)
1459 {
1460 int argc = *argcp;
1461 char **argv = *argvp;
1462 int ret;
1463
1464 if (strcmp(*argv, "time-soft") == 0) {
1465 NEXT_ARG();
1466 ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
1467 if (ret)
1468 invarg("value after \"time-soft\" is invalid", *argv);
1469 } else if (strcmp(*argv, "time-hard") == 0) {
1470 NEXT_ARG();
1471 ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
1472 if (ret)
1473 invarg("value after \"time-hard\" is invalid", *argv);
1474 } else if (strcmp(*argv, "time-use-soft") == 0) {
1475 NEXT_ARG();
1476 ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
1477 if (ret)
1478 invarg("value after \"time-use-soft\" is invalid", *argv);
1479 } else if (strcmp(*argv, "time-use-hard") == 0) {
1480 NEXT_ARG();
1481 ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
1482 if (ret)
1483 invarg("value after \"time-use-hard\" is invalid", *argv);
1484 } else if (strcmp(*argv, "byte-soft") == 0) {
1485 NEXT_ARG();
1486 ret = get_u64(&lft->soft_byte_limit, *argv, 0);
1487 if (ret)
1488 invarg("value after \"byte-soft\" is invalid", *argv);
1489 } else if (strcmp(*argv, "byte-hard") == 0) {
1490 NEXT_ARG();
1491 ret = get_u64(&lft->hard_byte_limit, *argv, 0);
1492 if (ret)
1493 invarg("value after \"byte-hard\" is invalid", *argv);
1494 } else if (strcmp(*argv, "packet-soft") == 0) {
1495 NEXT_ARG();
1496 ret = get_u64(&lft->soft_packet_limit, *argv, 0);
1497 if (ret)
1498 invarg("value after \"packet-soft\" is invalid", *argv);
1499 } else if (strcmp(*argv, "packet-hard") == 0) {
1500 NEXT_ARG();
1501 ret = get_u64(&lft->hard_packet_limit, *argv, 0);
1502 if (ret)
1503 invarg("value after \"packet-hard\" is invalid", *argv);
1504 } else
1505 invarg("LIMIT value is invalid", *argv);
1506
1507 *argcp = argc;
1508 *argvp = argv;
1509
1510 return 0;
1511 }
1512
1513 int do_xfrm(int argc, char **argv)
1514 {
1515 memset(&filter, 0, sizeof(filter));
1516
1517 if (argc < 1)
1518 usage();
1519
1520 if (matches(*argv, "state") == 0 ||
1521 matches(*argv, "sa") == 0)
1522 return do_xfrm_state(argc-1, argv+1);
1523 else if (matches(*argv, "policy") == 0)
1524 return do_xfrm_policy(argc-1, argv+1);
1525 else if (matches(*argv, "monitor") == 0)
1526 return do_xfrm_monitor(argc-1, argv+1);
1527 else if (matches(*argv, "help") == 0) {
1528 usage();
1529 fprintf(stderr, "xfrm Object \"%s\" is unknown.\n", *argv);
1530 exit(-1);
1531 }
1532 usage();
1533 }