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