]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/xfrm_state.c
id can be const char
[mirror_iproute2.git] / ip / xfrm_state.c
CommitLineData
c7699875 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20/*
21 * based on iproute.c
22 */
23/*
24 * Authors:
25 * Masahide NAKAMURA @USAGI
26 */
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <netdb.h>
32#include <linux/xfrm.h>
33#include "utils.h"
34#include "xfrm.h"
35#include "ip_common.h"
36
37//#define NLMSG_FLUSH_BUF_SIZE (4096-512)
38#define NLMSG_FLUSH_BUF_SIZE 8192
39
40/*
41 * Receiving buffer defines:
42 * nlmsg
43 * data = struct xfrm_usersa_info
44 * rtattr
45 * rtattr
46 * ... (max count of rtattr is XFRM_MAX_DEPTH)
47 *
48 * each rtattr data = struct xfrm_algo(dynamic size) or xfrm_address_t
49 */
50#define NLMSG_BUF_SIZE 4096
51#define RTA_BUF_SIZE 2048
52#define XFRM_ALGO_KEY_BUF_SIZE 512
53
54static void usage(void) __attribute__((noreturn));
55
56static void usage(void)
57{
58 fprintf(stderr, "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n");
59 fprintf(stderr, " [ reqid REQID ] [ FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ]\n");
60
61 fprintf(stderr, "Usage: ip xfrm state { delete | get } ID\n");
62 fprintf(stderr, "Usage: ip xfrm state { flush | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
63 fprintf(stderr, " [ FLAG_LIST ]\n");
64
65 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n");
9e566a46 66 //fprintf(stderr, "XFRM_PROTO := [ esp | ah | ipcomp ]\n");
67 fprintf(stderr, "XFRM_PROTO := [ ");
68 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ESP));
69 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_AH));
7809c616 70 fprintf(stderr, "%s ", strxf_proto(IPPROTO_COMP));
71 fprintf(stderr, "]\n");
9e566a46 72
c7699875 73 //fprintf(stderr, "SPI - security parameter index(default=0)\n");
74
75 fprintf(stderr, "MODE := [ transport | tunnel ](default=transport)\n");
76 //fprintf(stderr, "REQID - number(default=0)\n");
77
78 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] [ flag FLAG ]\n");
79 fprintf(stderr, "FLAG := [ noecn ]\n");
80
7809c616 81 fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n");
c7699875 82 fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n");
7809c616 83 fprintf(stderr, "ALGO_TYPE := [ ");
84 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT));
85 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AUTH));
86 fprintf(stderr, "%s ", strxf_algotype(XFRMA_ALG_COMP));
87 fprintf(stderr, "]\n");
88
c7699875 89 //fprintf(stderr, "ALGO_NAME - algorithm name\n");
90 //fprintf(stderr, "ALGO_KEY - algorithm key\n");
91
92 fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ upspec UPSPEC ] [ dev DEV ]\n");
93
94 fprintf(stderr, "UPSPEC := proto PROTO [ sport PORT ] [ dport PORT ]\n");
95
96 //fprintf(stderr, "DEV - device name(default=none)\n");
97 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n");
98 fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n");
99 fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n");
100 exit(-1);
101}
102
103static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
104 char *name, char *key, int max)
105{
106 int len;
7809c616 107 int slen = strlen(key);
c7699875 108
109#if 1
110 /* XXX: verifying both name and key is required! */
111 fprintf(stderr, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n");
112#endif
113
114 strncpy(alg->alg_name, name, sizeof(alg->alg_name));
115
7809c616 116 if (slen > 2 && strncmp(key, "0x", 2) == 0) {
c7699875 117 /*
118 * XXX: fix me!!
119 */
7809c616 120 union {
121 __u64 x;
122 unsigned char p[8];
123 } val;
124
125 memset(&val, 0, sizeof(val));
c7699875 126
7809c616 127 if (get_u64(&val.x, key, 16))
c7699875 128 invarg("\"ALGOKEY\" is invalid", key);
129
7809c616 130 len = (slen - 2) / 2;
c7699875 131 if (len > sizeof(val))
132 invarg("\"ALGOKEY\" is invalid: too large", key);
133
134 if (len > 0) {
7809c616 135 int i;
136
c7699875 137 if (len > max)
138 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
7809c616 139 for (i = sizeof(val.p) - 1; i >= 0; i--) {
140 int j = sizeof(val.p) - 1 - i;
141 alg->alg_key[j] = val.p[i];
142 }
c7699875 143 }
144
145 } else {
7809c616 146 len = slen;
c7699875 147 if (len > 0) {
148 if (len > max)
149 invarg("\"ALGOKEY\" makes buffer overflow\n", key);
150
151 strncpy(alg->alg_key, key, len);
152 }
153 }
154
155 alg->alg_key_len = len * 8;
156
157 return 0;
158}
159
160static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
161{
162 int argc = *argcp;
163 char **argv = *argvp;
9e566a46 164 int len = strlen(*argv);
165
166 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
167 __u8 val = 0;
c7699875 168
9e566a46 169 if (get_u8(&val, *argv, 16))
170 invarg("\"FLAG\" is invalid", *argv);
171 *flags = val;
172 } else {
173 if (strcmp(*argv, "noecn") == 0)
174 *flags |= XFRM_STATE_NOECN;
175 else
176 invarg("\"FLAG\" is invalid", *argv);
177 }
c7699875 178
179 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
180
181 *argcp = argc;
182 *argvp = argv;
183
184 return 0;
185}
186
187static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
188{
189 struct rtnl_handle rth;
190 struct {
191 struct nlmsghdr n;
192 struct xfrm_usersa_info xsinfo;
193 char buf[RTA_BUF_SIZE];
194 } req;
195 char *idp = NULL;
196 char *ealgop = NULL;
197 char *aalgop = NULL;
198 char *calgop = NULL;
199
200 memset(&req, 0, sizeof(req));
201
202 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
203 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
204 req.n.nlmsg_type = cmd;
205 req.xsinfo.family = preferred_family;
206
207 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
208 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
209 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
210 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
211
212 while (argc > 0) {
7809c616 213 if (strcmp(*argv, "mode") == 0) {
c7699875 214 NEXT_ARG();
215 xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
216 } else if (strcmp(*argv, "reqid") == 0) {
217 NEXT_ARG();
218 xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
219 } else if (strcmp(*argv, "flag") == 0) {
220 NEXT_ARG();
221 xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
222 } else if (strcmp(*argv, "sel") == 0) {
223 NEXT_ARG();
224 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
c7699875 225 } else if (strcmp(*argv, "limit") == 0) {
226 NEXT_ARG();
227 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
228 } else {
7809c616 229 /* try to assume ALGO */
230 int type = xfrm_algotype_getbyname(*argv);
231 switch (type) {
232 case XFRMA_ALG_CRYPT:
233 case XFRMA_ALG_AUTH:
234 case XFRMA_ALG_COMP:
235 {
236 /* ALGO */
237 struct {
238 struct xfrm_algo alg;
239 char buf[XFRM_ALGO_KEY_BUF_SIZE];
240 } alg;
241 int len;
242 char *name;
243 char *key;
244
245 switch (type) {
246 case XFRMA_ALG_CRYPT:
247 if (ealgop)
248 duparg("ALGOTYPE", *argv);
249 ealgop = *argv;
250 break;
251 case XFRMA_ALG_AUTH:
252 if (aalgop)
253 duparg("ALGOTYPE", *argv);
254 aalgop = *argv;
255 break;
256 case XFRMA_ALG_COMP:
257 if (calgop)
258 duparg("ALGOTYPE", *argv);
259 calgop = *argv;
260 break;
261 default:
262 /* not reached */
263 invarg("\"ALGOTYPE\" is invalid\n", *argv);
264 }
265
266 if (!NEXT_ARG_OK())
267 missarg("ALGONAME");
268 NEXT_ARG();
269 name = *argv;
270
271 if (!NEXT_ARG_OK())
272 missarg("ALGOKEY");
273 NEXT_ARG();
274 key = *argv;
275
276 memset(&alg, 0, sizeof(alg));
277
278 xfrm_algo_parse((void *)&alg, type, name, key,
279 sizeof(alg.buf));
280 len = sizeof(struct xfrm_algo) + alg.alg.alg_key_len;
281
282 addattr_l(&req.n, sizeof(req.buf), type,
283 (void *)&alg, len);
284 break;
285 }
286 default:
287 /* try to assume ID */
288 if (idp)
289 invarg("unknown", *argv);
290 idp = *argv;
291
292 /* ID */
293 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
294 &req.xsinfo.family, 0, &argc, &argv);
295 if (preferred_family == AF_UNSPEC)
296 preferred_family = req.xsinfo.family;
297 }
c7699875 298 }
299 argc--; argv++;
300 }
301
302 if (!idp) {
303 fprintf(stderr, "Not enough information: \"ID\" is required\n");
304 exit(1);
305 }
306
307 if (ealgop || aalgop || calgop) {
308 if (req.xsinfo.id.proto != IPPROTO_ESP &&
309 req.xsinfo.id.proto != IPPROTO_AH &&
310 req.xsinfo.id.proto != IPPROTO_COMP) {
7809c616 311 fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n", strxf_proto(req.xsinfo.id.proto));
c7699875 312 exit(1);
313 }
314 } else {
315 if (req.xsinfo.id.proto == IPPROTO_ESP ||
316 req.xsinfo.id.proto == IPPROTO_AH ||
317 req.xsinfo.id.proto == IPPROTO_COMP) {
7809c616 318 fprintf(stderr, "\"ALGO\" is required with proto=%s\n", strxf_proto(req.xsinfo.id.proto));
c7699875 319 exit (1);
320 }
321 }
322
323 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
324 exit(1);
325
326 if (req.xsinfo.family == AF_UNSPEC)
327 req.xsinfo.family = AF_INET;
328
329 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
330 exit(2);
331
332 rtnl_close(&rth);
333
334 return 0;
335}
336
337static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
338{
339 if (!filter.use)
340 return 1;
341
342 if (filter.id_src_mask)
343 if (memcmp(&xsinfo->saddr, &filter.xsinfo.saddr,
344 filter.id_src_mask) != 0)
345 return 0;
346 if (filter.id_dst_mask)
347 if (memcmp(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
348 filter.id_dst_mask) != 0)
349 return 0;
350 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
351 return 0;
352 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
353 return 0;
354 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
355 return 0;
356 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
357 return 0;
358 if (filter.state_flags_mask)
359 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
360 return 0;
361
362 return 1;
363}
364
7809c616 365static int xfrm_selector_iszero(struct xfrm_selector *s)
366{
367 struct xfrm_selector s0;
368
369 memset(&s0, 0, sizeof(s0));
370
371 return (memcmp(&s0, s, sizeof(s0)) == 0);
372}
373
c7699875 374int xfrm_state_print(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
375{
376 FILE *fp = (FILE*)arg;
377 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
378 int len = n->nlmsg_len;
379 struct rtattr * tb[XFRMA_MAX+1];
380 int ntb;
381
382 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
383 n->nlmsg_type != XFRM_MSG_DELSA) {
384 fprintf(stderr, "Not a state: %08x %08x %08x\n",
385 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
386 return 0;
387 }
388
389 len -= NLMSG_LENGTH(sizeof(*xsinfo));
390 if (len < 0) {
391 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
392 return -1;
393 }
394
395 if (!xfrm_state_filter_match(xsinfo))
396 return 0;
397
398 memset(tb, 0, sizeof(tb));
399 ntb = parse_rtattr_byindex(tb, XFRM_MAX_DEPTH, XFRMS_RTA(xsinfo), len);
400
401 if (n->nlmsg_type == XFRM_MSG_DELSA)
402 fprintf(fp, "Deleted ");
403
404 xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
405 xsinfo->reqid, xsinfo->family, fp, NULL);
406
9e566a46 407 fprintf(fp, "\t");
7809c616 408 fprintf(fp, "replay-window %d ", xsinfo->replay_window);
409 if (show_stats > 0)
c7699875 410 fprintf(fp, "seq 0x%08u ", xsinfo->seq);
7809c616 411 if (xsinfo->flags) {
412 fprintf(fp, "flag 0x%s", strxf_flags(xsinfo->flags));
413 if (show_stats > 0) {
414 if (xsinfo->flags) {
415 fprintf(fp, "(");
416 if (xsinfo->flags & XFRM_STATE_NOECN)
417 fprintf(fp, "noecn");
418 fprintf(fp, ")");
419 }
9e566a46 420 }
421 }
7809c616 422 fprintf(fp, "%s", _SL_);
c7699875 423
424 xfrm_xfrma_print(tb, ntb, xsinfo->family, fp, "\t");
425
7809c616 426 if (!xfrm_selector_iszero(&xsinfo->sel))
427 xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, "\tsel ");
c7699875 428
429 if (show_stats > 0) {
430 xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, "\t");
431 xfrm_stats_print(&xsinfo->stats, fp, "\t");
432 }
433
7809c616 434 if (oneline)
435 fprintf(fp, "\n");
436
c7699875 437 return 0;
438}
439
440static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
441{
442 struct rtnl_handle rth;
443 struct {
444 struct nlmsghdr n;
445 struct xfrm_usersa_id xsid;
446 } req;
447 struct xfrm_id id;
448 char *idp = NULL;
449
450 memset(&req, 0, sizeof(req));
451
452 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
453 req.n.nlmsg_flags = NLM_F_REQUEST;
454 req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
455 req.xsid.family = preferred_family;
456
457 while (argc > 0) {
458 /*
459 * XXX: Source address is not used and ignore it to follow
460 * XXX: a manner of setkey e.g. in the case of deleting/getting
461 * XXX: message of IPsec SA.
462 */
463 xfrm_address_t ignore_saddr;
464
465 if (idp)
466 invarg("unknown", *argv);
467 idp = *argv;
468
469 /* ID */
470 memset(&id, 0, sizeof(id));
7809c616 471 xfrm_id_parse(&ignore_saddr, &id, &req.xsid.family, 0,
c7699875 472 &argc, &argv);
473
474 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
475 req.xsid.spi = id.spi;
476 req.xsid.proto = id.proto;
477
478 argc--; argv++;
479 }
480
481 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
482 exit(1);
483
484 if (req.xsid.family == AF_UNSPEC)
485 req.xsid.family = AF_INET;
486
487 if (delete) {
488 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
489 exit(2);
490 } else {
491 char buf[NLMSG_BUF_SIZE];
492 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
493
494 memset(buf, 0, sizeof(buf));
495
496 if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0)
497 exit(2);
498
499 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
500 fprintf(stderr, "An error :-)\n");
501 exit(1);
502 }
503 }
504
505 rtnl_close(&rth);
506
507 return 0;
508}
509
510/*
511 * With an existing state of nlmsg, make new nlmsg for deleting the state
512 * and store it to buffer.
513 */
514int xfrm_state_keep(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
515{
516 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
517 struct rtnl_handle *rth = xb->rth;
518 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
519 int len = n->nlmsg_len;
520 struct nlmsghdr *new_n;
521 struct xfrm_usersa_id *xsid;
522
523 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
524 fprintf(stderr, "Not a state: %08x %08x %08x\n",
525 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
526 return 0;
527 }
528
529 len -= NLMSG_LENGTH(sizeof(*xsinfo));
530 if (len < 0) {
531 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
532 return -1;
533 }
534
535 if (!xfrm_state_filter_match(xsinfo))
536 return 0;
537
538 if (xb->offset > xb->size) {
539 fprintf(stderr, "Flush buffer overflow\n");
540 return -1;
541 }
542
543 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
544 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
545 new_n->nlmsg_flags = NLM_F_REQUEST;
546 new_n->nlmsg_type = XFRM_MSG_DELSA;
547 new_n->nlmsg_seq = ++rth->seq;
548
549 xsid = NLMSG_DATA(new_n);
550 xsid->family = xsinfo->family;
551 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
552 xsid->spi = xsinfo->id.spi;
553 xsid->proto = xsinfo->id.proto;
554
555 xb->offset += new_n->nlmsg_len;
556 xb->nlmsg_count ++;
557
558 return 0;
559}
560
561static int xfrm_state_list_or_flush(int argc, char **argv, int flush)
562{
563 char *idp = NULL;
564 struct rtnl_handle rth;
565
566 filter.use = 1;
567 filter.xsinfo.family = preferred_family;
568
569 while (argc > 0) {
570 if (strcmp(*argv, "mode") == 0) {
571 NEXT_ARG();
572 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
573
574 filter.mode_mask = XFRM_FILTER_MASK_FULL;
575
576 } else if (strcmp(*argv, "reqid") == 0) {
577 NEXT_ARG();
578 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
579
580 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
581
582 } else if (strcmp(*argv, "flag") == 0) {
583 NEXT_ARG();
584 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
585
586 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
587
588 } else {
589 if (idp)
590 invarg("unknown", *argv);
591 idp = *argv;
592
593 /* ID */
7809c616 594 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
595 &filter.xsinfo.family, 1, &argc, &argv);
c7699875 596 if (preferred_family == AF_UNSPEC)
597 preferred_family = filter.xsinfo.family;
598 }
599 argc--; argv++;
600 }
601
602 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
603 exit(1);
604
605 if (flush) {
606 struct xfrm_buffer xb;
607 char buf[NLMSG_FLUSH_BUF_SIZE];
608 int i;
609
610 xb.buf = buf;
611 xb.size = sizeof(buf);
612 xb.rth = &rth;
613
614 for (i = 0; ; i++) {
615 xb.offset = 0;
616 xb.nlmsg_count = 0;
617
618 if (show_stats > 1)
619 fprintf(stderr, "Flush round = %d\n", i);
620
621 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
622 perror("Cannot send dump request");
623 exit(1);
624 }
625
626 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) {
627 fprintf(stderr, "Flush terminated\n");
628 exit(1);
629 }
630 if (xb.nlmsg_count == 0) {
631 if (show_stats > 1)
632 fprintf(stderr, "Flush completed\n");
633 break;
634 }
635
636 if (rtnl_send(&rth, xb.buf, xb.offset) < 0) {
637 perror("Failed to send flush request\n");
638 exit(1);
639 }
640 if (show_stats > 1)
641 fprintf(stderr, "Flushed nlmsg count = %d\n", xb.nlmsg_count);
642
643 xb.offset = 0;
644 xb.nlmsg_count = 0;
645 }
646
647 } else {
648 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
649 perror("Cannot send dump request");
650 exit(1);
651 }
652
653 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) {
654 fprintf(stderr, "Dump terminated\n");
655 exit(1);
656 }
657 }
658
659 rtnl_close(&rth);
660
661 exit(0);
662}
663
664int do_xfrm_state(int argc, char **argv)
665{
666 if (argc < 1)
667 return xfrm_state_list_or_flush(0, NULL, 0);
668
669 if (matches(*argv, "add") == 0)
670 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
671 argc-1, argv+1);
672 if (matches(*argv, "update") == 0)
673 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
674 argc-1, argv+1);
675 if (matches(*argv, "delete") == 0 || matches(*argv, "del") == 0)
676 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
677 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
678 || matches(*argv, "lst") == 0)
679 return xfrm_state_list_or_flush(argc-1, argv+1, 0);
680 if (matches(*argv, "get") == 0)
681 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
682 if (matches(*argv, "flush") == 0)
683 return xfrm_state_list_or_flush(argc-1, argv+1, 1);
684 if (matches(*argv, "help") == 0)
685 usage();
686 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);
687 exit(-1);
688}