]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_state.c
ip/xfrm: Command syntax should not expect a key for compression
[mirror_iproute2.git] / ip / xfrm_state.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, 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_DELETEALL_BUF_SIZE (4096-512)
38 #define NLMSG_DELETEALL_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+1
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 #define CTX_BUF_SIZE 256
54
55 static void usage(void) __attribute__((noreturn));
56
57 static void usage(void)
58 {
59 fprintf(stderr, "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n");
60 fprintf(stderr, " [ mark MARK [ mask MASK ] ] [ reqid REQID ] [ seq SEQ ]\n");
61 fprintf(stderr, " [ replay-window SIZE ] [ replay-seq SEQ ] [ replay-oseq SEQ ]\n");
62 fprintf(stderr, " [ flag FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ] [ encap ENCAP ]\n");
63 fprintf(stderr, " [ coa ADDR[/PLEN] ] [ ctx CTX ]\n");
64 fprintf(stderr, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ mark MARK [ mask MASK ] ]\n");
65 fprintf(stderr, " [ reqid REQID ] [ seq SEQ ] [ min SPI max SPI ]\n");
66 fprintf(stderr, "Usage: ip xfrm state { delete | get } ID [ mark MARK [ mask MASK ] ]\n");
67 fprintf(stderr, "Usage: ip xfrm state { deleteall | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n");
68 fprintf(stderr, " [ flag FLAG-LIST ]\n");
69 fprintf(stderr, "Usage: ip xfrm state flush [ proto XFRM-PROTO ]\n");
70 fprintf(stderr, "Usage: ip xfrm state count\n");
71 fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM-PROTO ] [ spi SPI ]\n");
72 fprintf(stderr, "XFRM-PROTO := ");
73 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
74 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
75 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
76 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
77 fprintf(stderr, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS));
78 fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] ALGO\n");
79 fprintf(stderr, "ALGO := { ");
80 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT));
81 fprintf(stderr, "%s", strxf_algotype(XFRMA_ALG_AUTH));
82 fprintf(stderr, " } ALGO-NAME ALGO-KEY |\n");
83 fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_AUTH_TRUNC));
84 fprintf(stderr, " ALGO-NAME ALGO-KEY ALGO-TRUNC-LEN |\n");
85 fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_AEAD));
86 fprintf(stderr, " ALGO-NAME ALGO-KEY ALGO-ICV-LEN |\n");
87 fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_COMP));
88 fprintf(stderr, " ALGO-NAME\n");
89 fprintf(stderr, "MODE := transport | tunnel | ro | in_trigger | beet\n");
90 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
91 fprintf(stderr, "FLAG := noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec | align4\n");
92 fprintf(stderr, "SELECTOR := [ src ADDR[/PLEN] ] [ dst ADDR[/PLEN] ] [ dev DEV ] [ UPSPEC ]\n");
93 fprintf(stderr, "UPSPEC := proto { { ");
94 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_TCP));
95 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_UDP));
96 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_SCTP));
97 fprintf(stderr, "%s", strxf_proto(IPPROTO_DCCP));
98 fprintf(stderr, " } [ sport PORT ] [ dport PORT ] |\n");
99 fprintf(stderr, " { ");
100 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMP));
101 fprintf(stderr, "%s | ", strxf_proto(IPPROTO_ICMPV6));
102 fprintf(stderr, "%s", strxf_proto(IPPROTO_MH));
103 fprintf(stderr, " } [ type NUMBER ] [ code NUMBER ] |\n");
104 fprintf(stderr, " %s", strxf_proto(IPPROTO_GRE));
105 fprintf(stderr, " [ key { DOTTED-QUAD | NUMBER } ] | PROTO }\n");
106 fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] limit LIMIT\n");
107 fprintf(stderr, "LIMIT := { time-soft | time-hard | time-use-soft | time-use-hard } SECONDS |\n");
108 fprintf(stderr, " { byte-soft | byte-hard } SIZE | { packet-soft | packet-hard } COUNT\n");
109 fprintf(stderr, "ENCAP := { espinudp | espinudp-nonike } SPORT DPORT OADDR\n");
110
111 exit(-1);
112 }
113
114 static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
115 char *name, char *key, char *buf, int max)
116 {
117 int len;
118 int slen = strlen(key);
119
120 #if 0
121 /* XXX: verifying both name and key is required! */
122 fprintf(stderr, "warning: ALGO-NAME/ALGO-KEY will send to kernel promiscuously! (verifying them isn't implemented yet)\n");
123 #endif
124
125 strncpy(alg->alg_name, name, sizeof(alg->alg_name));
126
127 if (slen > 2 && strncmp(key, "0x", 2) == 0) {
128 /* split two chars "0x" from the top */
129 char *p = key + 2;
130 int plen = slen - 2;
131 int i;
132 int j;
133
134 /* Converting hexadecimal numbered string into real key;
135 * Convert each two chars into one char(value). If number
136 * of the length is odd, add zero on the top for rounding.
137 */
138
139 /* calculate length of the converted values(real key) */
140 len = (plen + 1) / 2;
141 if (len > max)
142 invarg("\"ALGO-KEY\" makes buffer overflow\n", key);
143
144 for (i = - (plen % 2), j = 0; j < len; i += 2, j++) {
145 char vbuf[3];
146 __u8 val;
147
148 vbuf[0] = i >= 0 ? p[i] : '0';
149 vbuf[1] = p[i + 1];
150 vbuf[2] = '\0';
151
152 if (get_u8(&val, vbuf, 16))
153 invarg("\"ALGO-KEY\" is invalid", key);
154
155 buf[j] = val;
156 }
157 } else {
158 len = slen;
159 if (len > 0) {
160 if (len > max)
161 invarg("\"ALGO-KEY\" makes buffer overflow\n", key);
162
163 strncpy(buf, key, len);
164 }
165 }
166
167 alg->alg_key_len = len * 8;
168
169 return 0;
170 }
171
172 static int xfrm_seq_parse(__u32 *seq, int *argcp, char ***argvp)
173 {
174 int argc = *argcp;
175 char **argv = *argvp;
176
177 if (get_u32(seq, *argv, 0))
178 invarg("\"SEQ\" is invalid", *argv);
179
180 *seq = htonl(*seq);
181
182 *argcp = argc;
183 *argvp = argv;
184
185 return 0;
186 }
187
188 static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
189 {
190 int argc = *argcp;
191 char **argv = *argvp;
192 int len = strlen(*argv);
193
194 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
195 __u8 val = 0;
196
197 if (get_u8(&val, *argv, 16))
198 invarg("\"FLAG\" is invalid", *argv);
199 *flags = val;
200 } else {
201 while (1) {
202 if (strcmp(*argv, "noecn") == 0)
203 *flags |= XFRM_STATE_NOECN;
204 else if (strcmp(*argv, "decap-dscp") == 0)
205 *flags |= XFRM_STATE_DECAP_DSCP;
206 else if (strcmp(*argv, "nopmtudisc") == 0)
207 *flags |= XFRM_STATE_NOPMTUDISC;
208 else if (strcmp(*argv, "wildrecv") == 0)
209 *flags |= XFRM_STATE_WILDRECV;
210 else if (strcmp(*argv, "icmp") == 0)
211 *flags |= XFRM_STATE_ICMP;
212 else if (strcmp(*argv, "af-unspec") == 0)
213 *flags |= XFRM_STATE_AF_UNSPEC;
214 else if (strcmp(*argv, "align4") == 0)
215 *flags |= XFRM_STATE_ALIGN4;
216 else {
217 PREV_ARG(); /* back track */
218 break;
219 }
220
221 if (!NEXT_ARG_OK())
222 break;
223 NEXT_ARG();
224 }
225 }
226
227 *argcp = argc;
228 *argvp = argv;
229
230 return 0;
231 }
232
233 static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv)
234 {
235 struct rtnl_handle rth;
236 struct {
237 struct nlmsghdr n;
238 struct xfrm_usersa_info xsinfo;
239 char buf[RTA_BUF_SIZE];
240 } req;
241 struct xfrm_replay_state replay;
242 char *idp = NULL;
243 char *aeadop = NULL;
244 char *ealgop = NULL;
245 char *aalgop = NULL;
246 char *calgop = NULL;
247 char *coap = NULL;
248 char *sctxp = NULL;
249 struct xfrm_mark mark = {0, 0};
250 struct {
251 struct xfrm_user_sec_ctx sctx;
252 char str[CTX_BUF_SIZE];
253 } ctx;
254
255 memset(&req, 0, sizeof(req));
256 memset(&replay, 0, sizeof(replay));
257 memset(&ctx, 0, sizeof(ctx));
258
259 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo));
260 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
261 req.n.nlmsg_type = cmd;
262 req.xsinfo.family = preferred_family;
263
264 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
265 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
266 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
267 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
268
269 while (argc > 0) {
270 if (strcmp(*argv, "mode") == 0) {
271 NEXT_ARG();
272 xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
273 } else if (strcmp(*argv, "mark") == 0) {
274 xfrm_parse_mark(&mark, &argc, &argv);
275 } else if (strcmp(*argv, "reqid") == 0) {
276 NEXT_ARG();
277 xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
278 } else if (strcmp(*argv, "seq") == 0) {
279 NEXT_ARG();
280 xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
281 } else if (strcmp(*argv, "replay-window") == 0) {
282 NEXT_ARG();
283 if (get_u8(&req.xsinfo.replay_window, *argv, 0))
284 invarg("\"replay-window\" value is invalid", *argv);
285 } else if (strcmp(*argv, "replay-seq") == 0) {
286 NEXT_ARG();
287 if (get_u32(&replay.seq, *argv, 0))
288 invarg("\"replay-seq\" value is invalid", *argv);
289 } else if (strcmp(*argv, "replay-oseq") == 0) {
290 NEXT_ARG();
291 if (get_u32(&replay.oseq, *argv, 0))
292 invarg("\"replay-oseq\" value is invalid", *argv);
293 } else if (strcmp(*argv, "flag") == 0) {
294 NEXT_ARG();
295 xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
296 } else if (strcmp(*argv, "sel") == 0) {
297 NEXT_ARG();
298 preferred_family = AF_UNSPEC;
299 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
300 preferred_family = req.xsinfo.sel.family;
301 } else if (strcmp(*argv, "limit") == 0) {
302 NEXT_ARG();
303 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
304 } else if (strcmp(*argv, "encap") == 0) {
305 struct xfrm_encap_tmpl encap;
306 inet_prefix oa;
307 NEXT_ARG();
308 xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
309 NEXT_ARG();
310 if (get_u16(&encap.encap_sport, *argv, 0))
311 invarg("\"encap\" sport value is invalid", *argv);
312 encap.encap_sport = htons(encap.encap_sport);
313 NEXT_ARG();
314 if (get_u16(&encap.encap_dport, *argv, 0))
315 invarg("\"encap\" dport value is invalid", *argv);
316 encap.encap_dport = htons(encap.encap_dport);
317 NEXT_ARG();
318 get_addr(&oa, *argv, AF_UNSPEC);
319 memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
320 addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
321 (void *)&encap, sizeof(encap));
322 } else if (strcmp(*argv, "coa") == 0) {
323 inet_prefix coa;
324 xfrm_address_t xcoa;
325
326 if (coap)
327 duparg("coa", *argv);
328 coap = *argv;
329
330 NEXT_ARG();
331
332 get_prefix(&coa, *argv, preferred_family);
333 if (coa.family == AF_UNSPEC)
334 invarg("\"coa\" address family is AF_UNSPEC", *argv);
335 if (coa.bytelen > sizeof(xcoa))
336 invarg("\"coa\" address length is too large", *argv);
337
338 memset(&xcoa, 0, sizeof(xcoa));
339 memcpy(&xcoa, &coa.data, coa.bytelen);
340
341 addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR,
342 (void *)&xcoa, sizeof(xcoa));
343 } else if (strcmp(*argv, "ctx") == 0) {
344 char *context;
345
346 if (sctxp)
347 duparg("ctx", *argv);
348 sctxp = *argv;
349
350 NEXT_ARG();
351 context = *argv;
352
353 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
354 addattr_l(&req.n, sizeof(req.buf), XFRMA_SEC_CTX,
355 (void *)&ctx, ctx.sctx.len);
356 } else {
357 /* try to assume ALGO */
358 int type = xfrm_algotype_getbyname(*argv);
359 switch (type) {
360 case XFRMA_ALG_AEAD:
361 case XFRMA_ALG_CRYPT:
362 case XFRMA_ALG_AUTH:
363 case XFRMA_ALG_AUTH_TRUNC:
364 case XFRMA_ALG_COMP:
365 {
366 /* ALGO */
367 struct {
368 union {
369 struct xfrm_algo alg;
370 struct xfrm_algo_aead aead;
371 struct xfrm_algo_auth auth;
372 } u;
373 char buf[XFRM_ALGO_KEY_BUF_SIZE];
374 } alg = {};
375 int len;
376 __u32 icvlen, trunclen;
377 char *name;
378 char *key = "";
379 char *buf;
380
381 switch (type) {
382 case XFRMA_ALG_AEAD:
383 if (ealgop || aalgop || aeadop)
384 duparg("ALGO-TYPE", *argv);
385 aeadop = *argv;
386 break;
387 case XFRMA_ALG_CRYPT:
388 if (ealgop || aeadop)
389 duparg("ALGO-TYPE", *argv);
390 ealgop = *argv;
391 break;
392 case XFRMA_ALG_AUTH:
393 case XFRMA_ALG_AUTH_TRUNC:
394 if (aalgop || aeadop)
395 duparg("ALGO-TYPE", *argv);
396 aalgop = *argv;
397 break;
398 case XFRMA_ALG_COMP:
399 if (calgop)
400 duparg("ALGO-TYPE", *argv);
401 calgop = *argv;
402 break;
403 default:
404 /* not reached */
405 invarg("\"ALGO-TYPE\" is invalid\n", *argv);
406 }
407
408 if (!NEXT_ARG_OK())
409 missarg("ALGO-NAME");
410 NEXT_ARG();
411 name = *argv;
412
413 switch (type) {
414 case XFRMA_ALG_AEAD:
415 case XFRMA_ALG_CRYPT:
416 case XFRMA_ALG_AUTH:
417 case XFRMA_ALG_AUTH_TRUNC:
418 if (!NEXT_ARG_OK())
419 missarg("ALGO-KEY");
420 NEXT_ARG();
421 key = *argv;
422 break;
423 }
424
425 buf = alg.u.alg.alg_key;
426 len = sizeof(alg.u.alg);
427
428 switch (type) {
429 case XFRMA_ALG_AEAD:
430 if (!NEXT_ARG_OK())
431 missarg("ALGO-ICV-LEN");
432 NEXT_ARG();
433 if (get_u32(&icvlen, *argv, 0))
434 invarg("\"aead\" ICV length is invalid",
435 *argv);
436 alg.u.aead.alg_icv_len = icvlen;
437
438 buf = alg.u.aead.alg_key;
439 len = sizeof(alg.u.aead);
440 break;
441 case XFRMA_ALG_AUTH_TRUNC:
442 if (!NEXT_ARG_OK())
443 missarg("ALGO-TRUNC-LEN");
444 NEXT_ARG();
445 if (get_u32(&trunclen, *argv, 0))
446 invarg("\"auth\" trunc length is invalid",
447 *argv);
448 alg.u.auth.alg_trunc_len = trunclen;
449
450 buf = alg.u.auth.alg_key;
451 len = sizeof(alg.u.auth);
452 break;
453 }
454
455 xfrm_algo_parse((void *)&alg, type, name, key,
456 buf, sizeof(alg.buf));
457 len += alg.u.alg.alg_key_len;
458
459 addattr_l(&req.n, sizeof(req.buf), type,
460 (void *)&alg, len);
461 break;
462 }
463 default:
464 /* try to assume ID */
465 if (idp)
466 invarg("unknown", *argv);
467 idp = *argv;
468
469 /* ID */
470 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
471 &req.xsinfo.family, 0, &argc, &argv);
472 if (preferred_family == AF_UNSPEC)
473 preferred_family = req.xsinfo.family;
474 }
475 }
476 argc--; argv++;
477 }
478
479 if (replay.seq || replay.oseq)
480 addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_VAL,
481 (void *)&replay, sizeof(replay));
482
483 if (!idp) {
484 fprintf(stderr, "Not enough information: \"ID\" is required\n");
485 exit(1);
486 }
487
488 if (mark.m & mark.v) {
489 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
490 (void *)&mark, sizeof(mark));
491 if (r < 0) {
492 fprintf(stderr, "XFRMA_MARK failed\n");
493 exit(1);
494 }
495 }
496
497 if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
498 switch (req.xsinfo.mode) {
499 case XFRM_MODE_TRANSPORT:
500 case XFRM_MODE_TUNNEL:
501 break;
502 case XFRM_MODE_BEET:
503 if (req.xsinfo.id.proto == IPPROTO_ESP)
504 break;
505 default:
506 fprintf(stderr, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
507 strxf_xfrmproto(req.xsinfo.id.proto));
508 exit(1);
509 }
510
511 switch (req.xsinfo.id.proto) {
512 case IPPROTO_ESP:
513 if (calgop) {
514 fprintf(stderr, "ALGO-TYPE value \"%s\" is invalid with XFRM-PROTO value \"%s\"\n",
515 strxf_algotype(XFRMA_ALG_COMP),
516 strxf_xfrmproto(req.xsinfo.id.proto));
517 exit(1);
518 }
519 if (!ealgop && !aeadop) {
520 fprintf(stderr, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
521 strxf_algotype(XFRMA_ALG_CRYPT),
522 strxf_algotype(XFRMA_ALG_AEAD),
523 strxf_xfrmproto(req.xsinfo.id.proto));
524 exit(1);
525 }
526 break;
527 case IPPROTO_AH:
528 if (ealgop || aeadop || calgop) {
529 fprintf(stderr, "ALGO-TYPE values \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
530 strxf_algotype(XFRMA_ALG_CRYPT),
531 strxf_algotype(XFRMA_ALG_AEAD),
532 strxf_algotype(XFRMA_ALG_COMP),
533 strxf_xfrmproto(req.xsinfo.id.proto));
534 exit(1);
535 }
536 if (!aalgop) {
537 fprintf(stderr, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
538 strxf_algotype(XFRMA_ALG_AUTH),
539 strxf_algotype(XFRMA_ALG_AUTH_TRUNC),
540 strxf_xfrmproto(req.xsinfo.id.proto));
541 exit(1);
542 }
543 break;
544 case IPPROTO_COMP:
545 if (ealgop || aalgop || aeadop) {
546 fprintf(stderr, "ALGO-TYPE values \"%s\", \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
547 strxf_algotype(XFRMA_ALG_CRYPT),
548 strxf_algotype(XFRMA_ALG_AUTH),
549 strxf_algotype(XFRMA_ALG_AUTH_TRUNC),
550 strxf_algotype(XFRMA_ALG_AEAD),
551 strxf_xfrmproto(req.xsinfo.id.proto));
552 exit(1);
553 }
554 if (!calgop) {
555 fprintf(stderr, "ALGO-TYPE value \"%s\" is required with XFRM-PROTO value \"%s\"\n",
556 strxf_algotype(XFRMA_ALG_COMP),
557 strxf_xfrmproto(req.xsinfo.id.proto));
558 exit(1);
559 }
560 break;
561 }
562 } else {
563 if (ealgop || aalgop || aeadop || calgop) {
564 fprintf(stderr, "ALGO is invalid with XFRM-PROTO value \"%s\"\n",
565 strxf_xfrmproto(req.xsinfo.id.proto));
566 exit(1);
567 }
568 }
569
570 if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
571 switch (req.xsinfo.mode) {
572 case XFRM_MODE_ROUTEOPTIMIZATION:
573 case XFRM_MODE_IN_TRIGGER:
574 break;
575 case 0:
576 fprintf(stderr, "\"mode\" is required with XFRM-PROTO value \"%s\"\n",
577 strxf_xfrmproto(req.xsinfo.id.proto));
578 exit(1);
579 default:
580 fprintf(stderr, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
581 strxf_xfrmproto(req.xsinfo.id.proto));
582 exit(1);
583 }
584
585 if (!coap) {
586 fprintf(stderr, "\"coa\" is required with XFRM-PROTO value \"%s\"\n",
587 strxf_xfrmproto(req.xsinfo.id.proto));
588 exit(1);
589 }
590 } else {
591 if (coap) {
592 fprintf(stderr, "\"coa\" is invalid with XFRM-PROTO value \"%s\"\n",
593 strxf_xfrmproto(req.xsinfo.id.proto));
594 exit(1);
595 }
596 }
597
598 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
599 exit(1);
600
601 if (req.xsinfo.family == AF_UNSPEC)
602 req.xsinfo.family = AF_INET;
603
604 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
605 exit(2);
606
607 rtnl_close(&rth);
608
609 return 0;
610 }
611
612 static int xfrm_state_allocspi(int argc, char **argv)
613 {
614 struct rtnl_handle rth;
615 struct {
616 struct nlmsghdr n;
617 struct xfrm_userspi_info xspi;
618 char buf[RTA_BUF_SIZE];
619 } req;
620 char *idp = NULL;
621 char *minp = NULL;
622 char *maxp = NULL;
623 struct xfrm_mark mark = {0, 0};
624 char res_buf[NLMSG_BUF_SIZE];
625 struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf;
626
627 memset(res_buf, 0, sizeof(res_buf));
628
629 memset(&req, 0, sizeof(req));
630
631 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi));
632 req.n.nlmsg_flags = NLM_F_REQUEST;
633 req.n.nlmsg_type = XFRM_MSG_ALLOCSPI;
634 req.xspi.info.family = preferred_family;
635
636 #if 0
637 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
638 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
639 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
640 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
641 #endif
642
643 while (argc > 0) {
644 if (strcmp(*argv, "mode") == 0) {
645 NEXT_ARG();
646 xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
647 } else if (strcmp(*argv, "mark") == 0) {
648 xfrm_parse_mark(&mark, &argc, &argv);
649 } else if (strcmp(*argv, "reqid") == 0) {
650 NEXT_ARG();
651 xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv);
652 } else if (strcmp(*argv, "seq") == 0) {
653 NEXT_ARG();
654 xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv);
655 } else if (strcmp(*argv, "min") == 0) {
656 if (minp)
657 duparg("min", *argv);
658 minp = *argv;
659
660 NEXT_ARG();
661
662 if (get_u32(&req.xspi.min, *argv, 0))
663 invarg("\"min\" value is invalid", *argv);
664 } else if (strcmp(*argv, "max") == 0) {
665 if (maxp)
666 duparg("max", *argv);
667 maxp = *argv;
668
669 NEXT_ARG();
670
671 if (get_u32(&req.xspi.max, *argv, 0))
672 invarg("\"max\" value is invalid", *argv);
673 } else {
674 /* try to assume ID */
675 if (idp)
676 invarg("unknown", *argv);
677 idp = *argv;
678
679 /* ID */
680 xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id,
681 &req.xspi.info.family, 0, &argc, &argv);
682 if (req.xspi.info.id.spi) {
683 fprintf(stderr, "\"SPI\" must be zero\n");
684 exit(1);
685 }
686 if (preferred_family == AF_UNSPEC)
687 preferred_family = req.xspi.info.family;
688 }
689 argc--; argv++;
690 }
691
692 if (!idp) {
693 fprintf(stderr, "Not enough information: \"ID\" is required\n");
694 exit(1);
695 }
696
697 if (minp) {
698 if (!maxp) {
699 fprintf(stderr, "\"max\" is missing\n");
700 exit(1);
701 }
702 if (req.xspi.min > req.xspi.max) {
703 fprintf(stderr, "\"min\" value is larger than \"max\" value\n");
704 exit(1);
705 }
706 } else {
707 if (maxp) {
708 fprintf(stderr, "\"min\" is missing\n");
709 exit(1);
710 }
711
712 /* XXX: Default value defined in PF_KEY;
713 * See kernel's net/key/af_key.c(pfkey_getspi).
714 */
715 req.xspi.min = 0x100;
716 req.xspi.max = 0x0fffffff;
717
718 /* XXX: IPCOMP spi is 16-bits;
719 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
720 */
721 if (req.xspi.info.id.proto == IPPROTO_COMP)
722 req.xspi.max = 0xffff;
723 }
724
725 if (mark.m & mark.v) {
726 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
727 (void *)&mark, sizeof(mark));
728 if (r < 0) {
729 fprintf(stderr, "XFRMA_MARK failed\n");
730 exit(1);
731 }
732 }
733
734 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
735 exit(1);
736
737 if (req.xspi.info.family == AF_UNSPEC)
738 req.xspi.info.family = AF_INET;
739
740
741 if (rtnl_talk(&rth, &req.n, 0, 0, res_n) < 0)
742 exit(2);
743
744 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
745 fprintf(stderr, "An error :-)\n");
746 exit(1);
747 }
748
749 rtnl_close(&rth);
750
751 return 0;
752 }
753
754 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
755 {
756 if (!filter.use)
757 return 1;
758
759 if (filter.id_src_mask)
760 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
761 filter.id_src_mask))
762 return 0;
763 if (filter.id_dst_mask)
764 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
765 filter.id_dst_mask))
766 return 0;
767 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
768 return 0;
769 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
770 return 0;
771 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
772 return 0;
773 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
774 return 0;
775 if (filter.state_flags_mask)
776 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
777 return 0;
778
779 return 1;
780 }
781
782 int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
783 void *arg)
784 {
785 FILE *fp = (FILE*)arg;
786 struct rtattr * tb[XFRMA_MAX+1];
787 struct rtattr * rta;
788 struct xfrm_usersa_info *xsinfo = NULL;
789 struct xfrm_user_expire *xexp = NULL;
790 struct xfrm_usersa_id *xsid = NULL;
791 int len = n->nlmsg_len;
792
793 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
794 n->nlmsg_type != XFRM_MSG_DELSA &&
795 n->nlmsg_type != XFRM_MSG_UPDSA &&
796 n->nlmsg_type != XFRM_MSG_EXPIRE) {
797 fprintf(stderr, "Not a state: %08x %08x %08x\n",
798 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
799 return 0;
800 }
801
802 if (n->nlmsg_type == XFRM_MSG_DELSA) {
803 /* Dont blame me for this .. Herbert made me do it */
804 xsid = NLMSG_DATA(n);
805 len -= NLMSG_SPACE(sizeof(*xsid));
806 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
807 xexp = NLMSG_DATA(n);
808 xsinfo = &xexp->state;
809 len -= NLMSG_SPACE(sizeof(*xexp));
810 } else {
811 xexp = NULL;
812 xsinfo = NLMSG_DATA(n);
813 len -= NLMSG_SPACE(sizeof(*xsinfo));
814 }
815
816 if (len < 0) {
817 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
818 return -1;
819 }
820
821 if (xsinfo && !xfrm_state_filter_match(xsinfo))
822 return 0;
823
824 if (n->nlmsg_type == XFRM_MSG_DELSA)
825 fprintf(fp, "Deleted ");
826 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
827 fprintf(fp, "Updated ");
828 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
829 fprintf(fp, "Expired ");
830
831 if (n->nlmsg_type == XFRM_MSG_DELSA)
832 rta = XFRMSID_RTA(xsid);
833 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
834 rta = XFRMEXP_RTA(xexp);
835 else
836 rta = XFRMS_RTA(xsinfo);
837
838 parse_rtattr(tb, XFRMA_MAX, rta, len);
839
840 if (n->nlmsg_type == XFRM_MSG_DELSA) {
841 //xfrm_policy_id_print();
842
843 if (!tb[XFRMA_SA]) {
844 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
845 return -1;
846 }
847 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
848 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
849 return -1;
850 }
851 xsinfo = RTA_DATA(tb[XFRMA_SA]);
852 }
853
854 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL);
855
856 if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
857 fprintf(fp, "\t");
858 fprintf(fp, "hard %u", xexp->hard);
859 fprintf(fp, "%s", _SL_);
860 }
861
862 if (oneline)
863 fprintf(fp, "\n");
864 fflush(fp);
865
866 return 0;
867 }
868
869 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
870 {
871 struct rtnl_handle rth;
872 struct {
873 struct nlmsghdr n;
874 struct xfrm_usersa_id xsid;
875 char buf[RTA_BUF_SIZE];
876 } req;
877 struct xfrm_id id;
878 char *idp = NULL;
879 struct xfrm_mark mark = {0, 0};
880
881 memset(&req, 0, sizeof(req));
882
883 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
884 req.n.nlmsg_flags = NLM_F_REQUEST;
885 req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
886 req.xsid.family = preferred_family;
887
888 while (argc > 0) {
889 xfrm_address_t saddr;
890
891 if (strcmp(*argv, "mark") == 0) {
892 xfrm_parse_mark(&mark, &argc, &argv);
893 } else {
894 if (idp)
895 invarg("unknown", *argv);
896 idp = *argv;
897
898 /* ID */
899 memset(&id, 0, sizeof(id));
900 memset(&saddr, 0, sizeof(saddr));
901 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
902 &argc, &argv);
903
904 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
905 req.xsid.spi = id.spi;
906 req.xsid.proto = id.proto;
907
908 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
909 (void *)&saddr, sizeof(saddr));
910 }
911
912 argc--; argv++;
913 }
914
915 if (mark.m & mark.v) {
916 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
917 (void *)&mark, sizeof(mark));
918 if (r < 0) {
919 fprintf(stderr, "XFRMA_MARK failed\n");
920 exit(1);
921 }
922 }
923
924 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
925 exit(1);
926
927 if (req.xsid.family == AF_UNSPEC)
928 req.xsid.family = AF_INET;
929
930 if (delete) {
931 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
932 exit(2);
933 } else {
934 char buf[NLMSG_BUF_SIZE];
935 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
936
937 memset(buf, 0, sizeof(buf));
938
939 if (rtnl_talk(&rth, &req.n, 0, 0, res_n) < 0)
940 exit(2);
941
942 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
943 fprintf(stderr, "An error :-)\n");
944 exit(1);
945 }
946 }
947
948 rtnl_close(&rth);
949
950 return 0;
951 }
952
953 /*
954 * With an existing state of nlmsg, make new nlmsg for deleting the state
955 * and store it to buffer.
956 */
957 static int xfrm_state_keep(const struct sockaddr_nl *who,
958 struct nlmsghdr *n,
959 void *arg)
960 {
961 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
962 struct rtnl_handle *rth = xb->rth;
963 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
964 int len = n->nlmsg_len;
965 struct nlmsghdr *new_n;
966 struct xfrm_usersa_id *xsid;
967
968 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
969 fprintf(stderr, "Not a state: %08x %08x %08x\n",
970 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
971 return 0;
972 }
973
974 len -= NLMSG_LENGTH(sizeof(*xsinfo));
975 if (len < 0) {
976 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
977 return -1;
978 }
979
980 if (!xfrm_state_filter_match(xsinfo))
981 return 0;
982
983 if (xb->offset > xb->size) {
984 fprintf(stderr, "State buffer overflow\n");
985 return -1;
986 }
987
988 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
989 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
990 new_n->nlmsg_flags = NLM_F_REQUEST;
991 new_n->nlmsg_type = XFRM_MSG_DELSA;
992 new_n->nlmsg_seq = ++rth->seq;
993
994 xsid = NLMSG_DATA(new_n);
995 xsid->family = xsinfo->family;
996 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
997 xsid->spi = xsinfo->id.spi;
998 xsid->proto = xsinfo->id.proto;
999
1000 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
1001 sizeof(xsid->daddr));
1002
1003 xb->offset += new_n->nlmsg_len;
1004 xb->nlmsg_count ++;
1005
1006 return 0;
1007 }
1008
1009 static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
1010 {
1011 char *idp = NULL;
1012 struct rtnl_handle rth;
1013
1014 if(argc > 0)
1015 filter.use = 1;
1016 filter.xsinfo.family = preferred_family;
1017
1018 while (argc > 0) {
1019 if (strcmp(*argv, "mode") == 0) {
1020 NEXT_ARG();
1021 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
1022
1023 filter.mode_mask = XFRM_FILTER_MASK_FULL;
1024
1025 } else if (strcmp(*argv, "reqid") == 0) {
1026 NEXT_ARG();
1027 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
1028
1029 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
1030
1031 } else if (strcmp(*argv, "flag") == 0) {
1032 NEXT_ARG();
1033 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
1034
1035 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
1036
1037 } else {
1038 if (idp)
1039 invarg("unknown", *argv);
1040 idp = *argv;
1041
1042 /* ID */
1043 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
1044 &filter.xsinfo.family, 1, &argc, &argv);
1045 if (preferred_family == AF_UNSPEC)
1046 preferred_family = filter.xsinfo.family;
1047 }
1048 argc--; argv++;
1049 }
1050
1051 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1052 exit(1);
1053
1054 if (deleteall) {
1055 struct xfrm_buffer xb;
1056 char buf[NLMSG_DELETEALL_BUF_SIZE];
1057 int i;
1058
1059 xb.buf = buf;
1060 xb.size = sizeof(buf);
1061 xb.rth = &rth;
1062
1063 for (i = 0; ; i++) {
1064 xb.offset = 0;
1065 xb.nlmsg_count = 0;
1066
1067 if (show_stats > 1)
1068 fprintf(stderr, "Delete-all round = %d\n", i);
1069
1070 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
1071 perror("Cannot send dump request");
1072 exit(1);
1073 }
1074
1075 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb) < 0) {
1076 fprintf(stderr, "Delete-all terminated\n");
1077 exit(1);
1078 }
1079 if (xb.nlmsg_count == 0) {
1080 if (show_stats > 1)
1081 fprintf(stderr, "Delete-all completed\n");
1082 break;
1083 }
1084
1085 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
1086 perror("Failed to send delete-all request\n");
1087 exit(1);
1088 }
1089 if (show_stats > 1)
1090 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
1091
1092 xb.offset = 0;
1093 xb.nlmsg_count = 0;
1094 }
1095
1096 } else {
1097 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
1098 perror("Cannot send dump request");
1099 exit(1);
1100 }
1101
1102 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout) < 0) {
1103 fprintf(stderr, "Dump terminated\n");
1104 exit(1);
1105 }
1106 }
1107
1108 rtnl_close(&rth);
1109
1110 exit(0);
1111 }
1112
1113 static int print_sadinfo(struct nlmsghdr *n, void *arg)
1114 {
1115 FILE *fp = (FILE*)arg;
1116 __u32 *f = NLMSG_DATA(n);
1117 struct rtattr *tb[XFRMA_SAD_MAX+1];
1118 struct rtattr *rta;
1119 __u32 *cnt;
1120
1121 int len = n->nlmsg_len;
1122
1123 len -= NLMSG_LENGTH(sizeof(__u32));
1124 if (len < 0) {
1125 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
1126 return -1;
1127 }
1128
1129 rta = XFRMSAPD_RTA(f);
1130 parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
1131
1132 if (tb[XFRMA_SAD_CNT]) {
1133 fprintf(fp,"\t SAD");
1134 cnt = (__u32 *)RTA_DATA(tb[XFRMA_SAD_CNT]);
1135 fprintf(fp," count %d", *cnt);
1136 } else {
1137 fprintf(fp,"BAD SAD info returned\n");
1138 return -1;
1139 }
1140
1141 if (show_stats) {
1142 if (tb[XFRMA_SAD_HINFO]) {
1143 struct xfrmu_sadhinfo *si;
1144
1145 if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) {
1146 fprintf(fp,"BAD SAD length returned\n");
1147 return -1;
1148 }
1149
1150 si = RTA_DATA(tb[XFRMA_SAD_HINFO]);
1151 fprintf(fp," (buckets ");
1152 fprintf(fp,"count %d", si->sadhcnt);
1153 fprintf(fp," Max %d", si->sadhmcnt);
1154 fprintf(fp,")");
1155 }
1156 }
1157 fprintf(fp,"\n");
1158
1159 return 0;
1160 }
1161
1162 static int xfrm_sad_getinfo(int argc, char **argv)
1163 {
1164 struct rtnl_handle rth;
1165 struct {
1166 struct nlmsghdr n;
1167 __u32 flags;
1168 char ans[64];
1169 } req;
1170
1171 memset(&req, 0, sizeof(req));
1172 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags));
1173 req.n.nlmsg_flags = NLM_F_REQUEST;
1174 req.n.nlmsg_type = XFRM_MSG_GETSADINFO;
1175 req.flags = 0XFFFFFFFF;
1176
1177 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1178 exit(1);
1179
1180 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0)
1181 exit(2);
1182
1183 print_sadinfo(&req.n, (void*)stdout);
1184
1185 rtnl_close(&rth);
1186
1187 return 0;
1188 }
1189
1190 static int xfrm_state_flush(int argc, char **argv)
1191 {
1192 struct rtnl_handle rth;
1193 struct {
1194 struct nlmsghdr n;
1195 struct xfrm_usersa_flush xsf;
1196 } req;
1197 char *protop = NULL;
1198
1199 memset(&req, 0, sizeof(req));
1200
1201 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
1202 req.n.nlmsg_flags = NLM_F_REQUEST;
1203 req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
1204 req.xsf.proto = 0;
1205
1206 while (argc > 0) {
1207 if (strcmp(*argv, "proto") == 0) {
1208 int ret;
1209
1210 if (protop)
1211 duparg("proto", *argv);
1212 protop = *argv;
1213
1214 NEXT_ARG();
1215
1216 ret = xfrm_xfrmproto_getbyname(*argv);
1217 if (ret < 0)
1218 invarg("\"XFRM-PROTO\" is invalid", *argv);
1219
1220 req.xsf.proto = (__u8)ret;
1221 } else
1222 invarg("unknown", *argv);
1223
1224 argc--; argv++;
1225 }
1226
1227 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1228 exit(1);
1229
1230 if (show_stats > 1)
1231 fprintf(stderr, "Flush state proto=%s\n",
1232 strxf_xfrmproto(req.xsf.proto));
1233
1234 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
1235 exit(2);
1236
1237 rtnl_close(&rth);
1238
1239 return 0;
1240 }
1241
1242 int do_xfrm_state(int argc, char **argv)
1243 {
1244 if (argc < 1)
1245 return xfrm_state_list_or_deleteall(0, NULL, 0);
1246
1247 if (matches(*argv, "add") == 0)
1248 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1249 argc-1, argv+1);
1250 if (matches(*argv, "update") == 0)
1251 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1252 argc-1, argv+1);
1253 if (matches(*argv, "allocspi") == 0)
1254 return xfrm_state_allocspi(argc-1, argv+1);
1255 if (matches(*argv, "delete") == 0)
1256 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
1257 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1258 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
1259 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1260 || matches(*argv, "lst") == 0)
1261 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
1262 if (matches(*argv, "get") == 0)
1263 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
1264 if (matches(*argv, "flush") == 0)
1265 return xfrm_state_flush(argc-1, argv+1);
1266 if (matches(*argv, "count") == 0) {
1267 return xfrm_sad_getinfo(argc, argv);
1268 }
1269 if (matches(*argv, "help") == 0)
1270 usage();
1271 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);
1272 exit(-1);
1273 }