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