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