1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
36#define NLMSG_DELETEALL_BUF_SIZE 8192
37
38
39
40
41
42
43
44
45
46
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 [ mask MASK ] ]\n"
66 " [ if_id IF_ID ] [ tfcpad LENGTH ]\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 | oseq-may-wrap\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
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
136 char *p = key + 2;
137 int plen = slen - 2;
138 int i;
139 int j;
140
141
142
143
144
145
146
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();
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 if (strcmp(*argv, "oseq-may-wrap") == 0)
257 *extra_flags |= XFRM_SA_XFLAG_OSEQ_MAY_WRAP;
258 else {
259 PREV_ARG();
260 break;
261 }
262
263 if (!NEXT_ARG_OK())
264 break;
265 NEXT_ARG();
266 }
267 }
268
269 *argcp = argc;
270 *argvp = argv;
271
272 return 0;
273}
274
275static int xfrm_offload_dir_parse(__u8 *dir, int *argcp, char ***argvp)
276{
277 int argc = *argcp;
278 char **argv = *argvp;
279
280 if (strcmp(*argv, "in") == 0)
281 *dir = XFRM_OFFLOAD_INBOUND;
282 else if (strcmp(*argv, "out") == 0)
283 *dir = 0;
284 else
285 invarg("DIR value is invalid", *argv);
286
287 *argcp = argc;
288 *argvp = argv;
289
290 return 0;
291}
292
293static int xfrm_state_modify(int cmd, unsigned int flags, int argc, char **argv)
294{
295 struct rtnl_handle rth;
296 struct {
297 struct nlmsghdr n;
298 struct xfrm_usersa_info xsinfo;
299 char buf[RTA_BUF_SIZE];
300 } req = {
301 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo)),
302 .n.nlmsg_flags = NLM_F_REQUEST | flags,
303 .n.nlmsg_type = cmd,
304 .xsinfo.family = preferred_family,
305 .xsinfo.lft.soft_byte_limit = XFRM_INF,
306 .xsinfo.lft.hard_byte_limit = XFRM_INF,
307 .xsinfo.lft.soft_packet_limit = XFRM_INF,
308 .xsinfo.lft.hard_packet_limit = XFRM_INF,
309 };
310 struct xfrm_replay_state replay = {};
311 struct xfrm_replay_state_esn replay_esn = {};
312 struct xfrm_user_offload xuo = {};
313 unsigned int ifindex = 0;
314 __u8 dir = 0;
315 bool is_offload = false;
316 __u32 replay_window = 0;
317 __u32 seq = 0, oseq = 0, seq_hi = 0, oseq_hi = 0;
318 char *idp = NULL;
319 char *aeadop = NULL;
320 char *ealgop = NULL;
321 char *aalgop = NULL;
322 char *calgop = NULL;
323 char *coap = NULL;
324 char *sctxp = NULL;
325 __u32 extra_flags = 0;
326 struct xfrm_mark mark = {0, 0};
327 struct {
328 struct xfrm_user_sec_ctx sctx;
329 char str[CTX_BUF_SIZE];
330 } ctx = {};
331 struct xfrm_mark output_mark = {0, 0};
332 bool is_if_id_set = false;
333 __u32 if_id = 0;
334 __u32 tfcpad = 0;
335
336 while (argc > 0) {
337 if (strcmp(*argv, "mode") == 0) {
338 NEXT_ARG();
339 xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
340 } else if (strcmp(*argv, "mark") == 0) {
341 xfrm_parse_mark(&mark, &argc, &argv);
342 } else if (strcmp(*argv, "reqid") == 0) {
343 NEXT_ARG();
344 xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
345 } else if (strcmp(*argv, "seq") == 0) {
346 NEXT_ARG();
347 xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
348 } else if (strcmp(*argv, "replay-window") == 0) {
349 NEXT_ARG();
350 if (get_u32(&replay_window, *argv, 0))
351 invarg("value after \"replay-window\" is invalid", *argv);
352 } else if (strcmp(*argv, "replay-seq") == 0) {
353 NEXT_ARG();
354 if (get_u32(&seq, *argv, 0))
355 invarg("value after \"replay-seq\" is invalid", *argv);
356 } else if (strcmp(*argv, "replay-seq-hi") == 0) {
357 NEXT_ARG();
358 if (get_u32(&seq_hi, *argv, 0))
359 invarg("value after \"replay-seq-hi\" is invalid", *argv);
360 } else if (strcmp(*argv, "replay-oseq") == 0) {
361 NEXT_ARG();
362 if (get_u32(&oseq, *argv, 0))
363 invarg("value after \"replay-oseq\" is invalid", *argv);
364 } else if (strcmp(*argv, "replay-oseq-hi") == 0) {
365 NEXT_ARG();
366 if (get_u32(&oseq_hi, *argv, 0))
367 invarg("value after \"replay-oseq-hi\" is invalid", *argv);
368 } else if (strcmp(*argv, "flag") == 0) {
369 NEXT_ARG();
370 xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
371 } else if (strcmp(*argv, "extra-flag") == 0) {
372 NEXT_ARG();
373 xfrm_state_extra_flag_parse(&extra_flags, &argc, &argv);
374 } else if (strcmp(*argv, "sel") == 0) {
375 NEXT_ARG();
376 preferred_family = AF_UNSPEC;
377 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
378 preferred_family = req.xsinfo.sel.family;
379 } else if (strcmp(*argv, "limit") == 0) {
380 NEXT_ARG();
381 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
382 } else if (strcmp(*argv, "encap") == 0) {
383 struct xfrm_encap_tmpl encap;
384 inet_prefix oa;
385 NEXT_ARG();
386 xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
387 NEXT_ARG();
388 if (get_be16(&encap.encap_sport, *argv, 0))
389 invarg("SPORT value after \"encap\" is invalid", *argv);
390 NEXT_ARG();
391 if (get_be16(&encap.encap_dport, *argv, 0))
392 invarg("DPORT value after \"encap\" is invalid", *argv);
393 NEXT_ARG();
394 get_addr(&oa, *argv, AF_UNSPEC);
395 memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
396 addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
397 (void *)&encap, sizeof(encap));
398 } else if (strcmp(*argv, "coa") == 0) {
399 inet_prefix coa;
400 xfrm_address_t xcoa = {};
401
402 if (coap)
403 duparg("coa", *argv);
404 coap = *argv;
405
406 NEXT_ARG();
407
408 get_prefix(&coa, *argv, preferred_family);
409 if (coa.family == AF_UNSPEC)
410 invarg("value after \"coa\" has an unrecognized address family", *argv);
411 if (coa.bytelen > sizeof(xcoa))
412 invarg("value after \"coa\" is too large", *argv);
413
414 memcpy(&xcoa, &coa.data, coa.bytelen);
415
416 addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR,
417 (void *)&xcoa, sizeof(xcoa));
418 } else if (strcmp(*argv, "ctx") == 0) {
419 char *context;
420
421 if (sctxp)
422 duparg("ctx", *argv);
423 sctxp = *argv;
424
425 NEXT_ARG();
426 context = *argv;
427
428 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
429 addattr_l(&req.n, sizeof(req.buf), XFRMA_SEC_CTX,
430 (void *)&ctx, ctx.sctx.len);
431 } else if (strcmp(*argv, "offload") == 0) {
432 is_offload = true;
433 NEXT_ARG();
434 if (strcmp(*argv, "dev") == 0) {
435 NEXT_ARG();
436 ifindex = ll_name_to_index(*argv);
437 if (!ifindex) {
438 invarg("value after \"offload dev\" is invalid", *argv);
439 is_offload = false;
440 }
441 NEXT_ARG();
442 }
443 if (strcmp(*argv, "dir") == 0) {
444 NEXT_ARG();
445 xfrm_offload_dir_parse(&dir, &argc, &argv);
446 } else {
447 invarg("value after \"offload dir\" is invalid", *argv);
448 is_offload = false;
449 }
450 } else if (strcmp(*argv, "output-mark") == 0) {
451 NEXT_ARG();
452 if (get_u32(&output_mark.v, *argv, 0))
453 invarg("value after \"output-mark\" is invalid", *argv);
454 if (argc > 1) {
455 NEXT_ARG();
456 if (strcmp(*argv, "mask") == 0) {
457 NEXT_ARG();
458 if (get_u32(&output_mark.m, *argv, 0))
459 invarg("mask value is invalid\n", *argv);
460 } else {
461 PREV_ARG();
462 }
463 }
464 } else if (strcmp(*argv, "if_id") == 0) {
465 NEXT_ARG();
466 if (get_u32(&if_id, *argv, 0))
467 invarg("value after \"if_id\" is invalid", *argv);
468 is_if_id_set = true;
469 } else if (strcmp(*argv, "tfcpad") == 0) {
470 NEXT_ARG();
471 if (get_u32(&tfcpad, *argv, 0))
472 invarg("value after \"tfcpad\" is invalid", *argv);
473 } else {
474
475 int type = xfrm_algotype_getbyname(*argv);
476
477 switch (type) {
478 case XFRMA_ALG_AEAD:
479 case XFRMA_ALG_CRYPT:
480 case XFRMA_ALG_AUTH:
481 case XFRMA_ALG_AUTH_TRUNC:
482 case XFRMA_ALG_COMP:
483 {
484
485 struct {
486 union {
487 struct xfrm_algo alg;
488 struct xfrm_algo_aead aead;
489 struct xfrm_algo_auth auth;
490 } u;
491 char buf[XFRM_ALGO_KEY_BUF_SIZE];
492 } alg = {};
493 int len;
494 __u32 icvlen, trunclen;
495 char *name;
496 char *key = "";
497 char *buf;
498
499 switch (type) {
500 case XFRMA_ALG_AEAD:
501 if (ealgop || aalgop || aeadop)
502 duparg("ALGO-TYPE", *argv);
503 aeadop = *argv;
504 break;
505 case XFRMA_ALG_CRYPT:
506 if (ealgop || aeadop)
507 duparg("ALGO-TYPE", *argv);
508 ealgop = *argv;
509 break;
510 case XFRMA_ALG_AUTH:
511 case XFRMA_ALG_AUTH_TRUNC:
512 if (aalgop || aeadop)
513 duparg("ALGO-TYPE", *argv);
514 aalgop = *argv;
515 break;
516 case XFRMA_ALG_COMP:
517 if (calgop)
518 duparg("ALGO-TYPE", *argv);
519 calgop = *argv;
520 break;
521 default:
522
523 invarg("ALGO-TYPE value is invalid\n", *argv);
524 }
525
526 if (!NEXT_ARG_OK())
527 missarg("ALGO-NAME");
528 NEXT_ARG();
529 name = *argv;
530
531 switch (type) {
532 case XFRMA_ALG_AEAD:
533 case XFRMA_ALG_CRYPT:
534 case XFRMA_ALG_AUTH:
535 case XFRMA_ALG_AUTH_TRUNC:
536 if (!NEXT_ARG_OK())
537 missarg("ALGO-KEYMAT");
538 NEXT_ARG();
539 key = *argv;
540 break;
541 }
542
543 buf = alg.u.alg.alg_key;
544 len = sizeof(alg.u.alg);
545
546 switch (type) {
547 case XFRMA_ALG_AEAD:
548 if (!NEXT_ARG_OK())
549 missarg("ALGO-ICV-LEN");
550 NEXT_ARG();
551 if (get_u32(&icvlen, *argv, 0))
552 invarg("ALGO-ICV-LEN value is invalid",
553 *argv);
554 alg.u.aead.alg_icv_len = icvlen;
555
556 buf = alg.u.aead.alg_key;
557 len = sizeof(alg.u.aead);
558 break;
559 case XFRMA_ALG_AUTH_TRUNC:
560 if (!NEXT_ARG_OK())
561 missarg("ALGO-TRUNC-LEN");
562 NEXT_ARG();
563 if (get_u32(&trunclen, *argv, 0))
564 invarg("ALGO-TRUNC-LEN value is invalid",
565 *argv);
566 alg.u.auth.alg_trunc_len = trunclen;
567
568 buf = alg.u.auth.alg_key;
569 len = sizeof(alg.u.auth);
570 break;
571 }
572
573 xfrm_algo_parse((void *)&alg, type, name, key,
574 buf, sizeof(alg.buf));
575 len += alg.u.alg.alg_key_len / 8;
576
577 addattr_l(&req.n, sizeof(req.buf), type,
578 (void *)&alg, len);
579 break;
580 }
581 default:
582
583 if (idp)
584 invarg("unknown", *argv);
585 idp = *argv;
586
587
588 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
589 &req.xsinfo.family, 0, &argc, &argv);
590 if (preferred_family == AF_UNSPEC)
591 preferred_family = req.xsinfo.family;
592 }
593 }
594 argc--; argv++;
595 }
596
597 if (req.xsinfo.flags & XFRM_STATE_ESN &&
598 replay_window == 0) {
599 fprintf(stderr, "Error: esn flag set without replay-window.\n");
600 exit(-1);
601 }
602
603 if (replay_window > XFRMA_REPLAY_ESN_MAX) {
604 fprintf(stderr,
605 "Error: replay-window (%u) > XFRMA_REPLAY_ESN_MAX (%u).\n",
606 replay_window, XFRMA_REPLAY_ESN_MAX);
607 exit(-1);
608 }
609
610 if (is_offload) {
611 xuo.ifindex = ifindex;
612 xuo.flags = dir;
613 addattr_l(&req.n, sizeof(req.buf), XFRMA_OFFLOAD_DEV, &xuo,
614 sizeof(xuo));
615 }
616 if (req.xsinfo.flags & XFRM_STATE_ESN ||
617 replay_window > (sizeof(replay.bitmap) * 8)) {
618 replay_esn.seq = seq;
619 replay_esn.oseq = oseq;
620 replay_esn.seq_hi = seq_hi;
621 replay_esn.oseq_hi = oseq_hi;
622 replay_esn.replay_window = replay_window;
623 replay_esn.bmp_len = (replay_window + sizeof(__u32) * 8 - 1) /
624 (sizeof(__u32) * 8);
625 addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_ESN_VAL,
626 &replay_esn, sizeof(replay_esn));
627 } else {
628 if (seq || oseq) {
629 replay.seq = seq;
630 replay.oseq = oseq;
631 addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_VAL,
632 &replay, sizeof(replay));
633 }
634 req.xsinfo.replay_window = replay_window;
635 }
636
637 if (extra_flags)
638 addattr32(&req.n, sizeof(req.buf), XFRMA_SA_EXTRA_FLAGS,
639 extra_flags);
640
641 if (!idp) {
642 fprintf(stderr, "Not enough information: ID is required\n");
643 exit(1);
644 }
645
646 if (mark.m) {
647 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
648 (void *)&mark, sizeof(mark));
649 if (r < 0) {
650 fprintf(stderr, "XFRMA_MARK failed\n");
651 exit(1);
652 }
653 }
654
655 if (is_if_id_set)
656 addattr32(&req.n, sizeof(req.buf), XFRMA_IF_ID, if_id);
657
658 if (tfcpad)
659 addattr32(&req.n, sizeof(req.buf), XFRMA_TFCPAD, tfcpad);
660
661 if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
662 switch (req.xsinfo.mode) {
663 case XFRM_MODE_TRANSPORT:
664 case XFRM_MODE_TUNNEL:
665 break;
666 case XFRM_MODE_BEET:
667 if (req.xsinfo.id.proto == IPPROTO_ESP)
668 break;
669 default:
670 fprintf(stderr, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
671 strxf_xfrmproto(req.xsinfo.id.proto));
672 exit(1);
673 }
674
675 switch (req.xsinfo.id.proto) {
676 case IPPROTO_ESP:
677 if (calgop) {
678 fprintf(stderr, "ALGO-TYPE value \"%s\" is invalid with XFRM-PROTO value \"%s\"\n",
679 strxf_algotype(XFRMA_ALG_COMP),
680 strxf_xfrmproto(req.xsinfo.id.proto));
681 exit(1);
682 }
683 if (!ealgop && !aeadop) {
684 fprintf(stderr, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
685 strxf_algotype(XFRMA_ALG_CRYPT),
686 strxf_algotype(XFRMA_ALG_AEAD),
687 strxf_xfrmproto(req.xsinfo.id.proto));
688 exit(1);
689 }
690 break;
691 case IPPROTO_AH:
692 if (ealgop || aeadop || calgop) {
693 fprintf(stderr, "ALGO-TYPE values \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
694 strxf_algotype(XFRMA_ALG_CRYPT),
695 strxf_algotype(XFRMA_ALG_AEAD),
696 strxf_algotype(XFRMA_ALG_COMP),
697 strxf_xfrmproto(req.xsinfo.id.proto));
698 exit(1);
699 }
700 if (!aalgop) {
701 fprintf(stderr, "ALGO-TYPE value \"%s\" or \"%s\" is required with XFRM-PROTO value \"%s\"\n",
702 strxf_algotype(XFRMA_ALG_AUTH),
703 strxf_algotype(XFRMA_ALG_AUTH_TRUNC),
704 strxf_xfrmproto(req.xsinfo.id.proto));
705 exit(1);
706 }
707 break;
708 case IPPROTO_COMP:
709 if (ealgop || aalgop || aeadop) {
710 fprintf(stderr, "ALGO-TYPE values \"%s\", \"%s\", \"%s\", and \"%s\" are invalid with XFRM-PROTO value \"%s\"\n",
711 strxf_algotype(XFRMA_ALG_CRYPT),
712 strxf_algotype(XFRMA_ALG_AUTH),
713 strxf_algotype(XFRMA_ALG_AUTH_TRUNC),
714 strxf_algotype(XFRMA_ALG_AEAD),
715 strxf_xfrmproto(req.xsinfo.id.proto));
716 exit(1);
717 }
718 if (!calgop) {
719 fprintf(stderr, "ALGO-TYPE value \"%s\" is required with XFRM-PROTO value \"%s\"\n",
720 strxf_algotype(XFRMA_ALG_COMP),
721 strxf_xfrmproto(req.xsinfo.id.proto));
722 exit(1);
723 }
724 break;
725 }
726 } else {
727 if (ealgop || aalgop || aeadop || calgop) {
728 fprintf(stderr, "ALGO is invalid with XFRM-PROTO value \"%s\"\n",
729 strxf_xfrmproto(req.xsinfo.id.proto));
730 exit(1);
731 }
732 }
733
734 if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
735 switch (req.xsinfo.mode) {
736 case XFRM_MODE_ROUTEOPTIMIZATION:
737 case XFRM_MODE_IN_TRIGGER:
738 break;
739 case 0:
740 fprintf(stderr, "\"mode\" is required with XFRM-PROTO value \"%s\"\n",
741 strxf_xfrmproto(req.xsinfo.id.proto));
742 exit(1);
743 default:
744 fprintf(stderr, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
745 strxf_xfrmproto(req.xsinfo.id.proto));
746 exit(1);
747 }
748
749 if (!coap) {
750 fprintf(stderr, "\"coa\" is required with XFRM-PROTO value \"%s\"\n",
751 strxf_xfrmproto(req.xsinfo.id.proto));
752 exit(1);
753 }
754 } else {
755 if (coap) {
756 fprintf(stderr, "\"coa\" is invalid with XFRM-PROTO value \"%s\"\n",
757 strxf_xfrmproto(req.xsinfo.id.proto));
758 exit(1);
759 }
760 }
761
762 if (output_mark.v)
763 addattr32(&req.n, sizeof(req.buf), XFRMA_OUTPUT_MARK, output_mark.v);
764
765 if (output_mark.m)
766 addattr32(&req.n, sizeof(req.buf), XFRMA_SET_MARK_MASK, output_mark.m);
767
768 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
769 exit(1);
770
771 if (req.xsinfo.family == AF_UNSPEC)
772 req.xsinfo.family = AF_INET;
773
774 if (rtnl_talk(&rth, &req.n, NULL) < 0)
775 exit(2);
776
777 rtnl_close(&rth);
778
779 return 0;
780}
781
782static int xfrm_state_allocspi(int argc, char **argv)
783{
784 struct rtnl_handle rth;
785 struct {
786 struct nlmsghdr n;
787 struct xfrm_userspi_info xspi;
788 char buf[RTA_BUF_SIZE];
789 } req = {
790 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi)),
791 .n.nlmsg_flags = NLM_F_REQUEST,
792 .n.nlmsg_type = XFRM_MSG_ALLOCSPI,
793 .xspi.info.family = preferred_family,
794#if 0
795 .xspi.lft.soft_byte_limit = XFRM_INF,
796 .xspi.lft.hard_byte_limit = XFRM_INF,
797 .xspi.lft.soft_packet_limit = XFRM_INF,
798 .xspi.lft.hard_packet_limit = XFRM_INF,
799#endif
800 };
801 char *idp = NULL;
802 char *minp = NULL;
803 char *maxp = NULL;
804 struct xfrm_mark mark = {0, 0};
805 struct nlmsghdr *answer;
806
807 while (argc > 0) {
808 if (strcmp(*argv, "mode") == 0) {
809 NEXT_ARG();
810 xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
811 } else if (strcmp(*argv, "mark") == 0) {
812 xfrm_parse_mark(&mark, &argc, &argv);
813 } else if (strcmp(*argv, "reqid") == 0) {
814 NEXT_ARG();
815 xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv);
816 } else if (strcmp(*argv, "seq") == 0) {
817 NEXT_ARG();
818 xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv);
819 } else if (strcmp(*argv, "min") == 0) {
820 if (minp)
821 duparg("min", *argv);
822 minp = *argv;
823
824 NEXT_ARG();
825
826 if (get_u32(&req.xspi.min, *argv, 0))
827 invarg("value after \"min\" is invalid", *argv);
828 } else if (strcmp(*argv, "max") == 0) {
829 if (maxp)
830 duparg("max", *argv);
831 maxp = *argv;
832
833 NEXT_ARG();
834
835 if (get_u32(&req.xspi.max, *argv, 0))
836 invarg("value after \"max\" is invalid", *argv);
837 } else {
838
839 if (idp)
840 invarg("unknown", *argv);
841 idp = *argv;
842
843
844 xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id,
845 &req.xspi.info.family, 0, &argc, &argv);
846 if (req.xspi.info.id.spi) {
847 fprintf(stderr, "\"spi\" is invalid\n");
848 exit(1);
849 }
850 if (preferred_family == AF_UNSPEC)
851 preferred_family = req.xspi.info.family;
852 }
853 argc--; argv++;
854 }
855
856 if (!idp) {
857 fprintf(stderr, "Not enough information: ID is required\n");
858 exit(1);
859 }
860
861 if (minp) {
862 if (!maxp) {
863 fprintf(stderr, "\"max\" is missing\n");
864 exit(1);
865 }
866 if (req.xspi.min > req.xspi.max) {
867 fprintf(stderr, "value after \"min\" is larger than value after \"max\"\n");
868 exit(1);
869 }
870 } else {
871 if (maxp) {
872 fprintf(stderr, "\"min\" is missing\n");
873 exit(1);
874 }
875
876
877
878
879 req.xspi.min = 0x100;
880 req.xspi.max = 0x0fffffff;
881
882
883
884
885 if (req.xspi.info.id.proto == IPPROTO_COMP)
886 req.xspi.max = 0xffff;
887 }
888
889 if (mark.m & mark.v) {
890 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
891 (void *)&mark, sizeof(mark));
892 if (r < 0) {
893 fprintf(stderr, "XFRMA_MARK failed\n");
894 exit(1);
895 }
896 }
897
898 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
899 exit(1);
900
901 if (req.xspi.info.family == AF_UNSPEC)
902 req.xspi.info.family = AF_INET;
903
904
905 if (rtnl_talk(&rth, &req.n, &answer) < 0)
906 exit(2);
907
908 if (xfrm_state_print(answer, (void *)stdout) < 0) {
909 fprintf(stderr, "An error :-)\n");
910 exit(1);
911 }
912
913 free(answer);
914 rtnl_close(&rth);
915
916 return 0;
917}
918
919static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
920{
921 if (!filter.use)
922 return 1;
923
924 if (filter.xsinfo.family != AF_UNSPEC &&
925 filter.xsinfo.family != xsinfo->family)
926 return 0;
927
928 if (filter.id_src_mask)
929 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
930 filter.id_src_mask))
931 return 0;
932 if (filter.id_dst_mask)
933 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
934 filter.id_dst_mask))
935 return 0;
936 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
937 return 0;
938 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
939 return 0;
940 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
941 return 0;
942 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
943 return 0;
944 if (filter.state_flags_mask)
945 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
946 return 0;
947
948 return 1;
949}
950
951static int __do_xfrm_state_print(struct nlmsghdr *n, void *arg, bool nokeys)
952{
953 FILE *fp = (FILE *)arg;
954 struct rtattr *tb[XFRMA_MAX+1];
955 struct rtattr *rta;
956 struct xfrm_usersa_info *xsinfo = NULL;
957 struct xfrm_user_expire *xexp = NULL;
958 struct xfrm_usersa_id *xsid = NULL;
959 int len = n->nlmsg_len;
960
961 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
962 n->nlmsg_type != XFRM_MSG_DELSA &&
963 n->nlmsg_type != XFRM_MSG_UPDSA &&
964 n->nlmsg_type != XFRM_MSG_EXPIRE) {
965 fprintf(stderr, "Not a state: %08x %08x %08x\n",
966 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
967 return 0;
968 }
969
970 if (n->nlmsg_type == XFRM_MSG_DELSA) {
971
972 xsid = NLMSG_DATA(n);
973 len -= NLMSG_SPACE(sizeof(*xsid));
974 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
975 xexp = NLMSG_DATA(n);
976 xsinfo = &xexp->state;
977 len -= NLMSG_SPACE(sizeof(*xexp));
978 } else {
979 xexp = NULL;
980 xsinfo = NLMSG_DATA(n);
981 len -= NLMSG_SPACE(sizeof(*xsinfo));
982 }
983
984 if (len < 0) {
985 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
986 return -1;
987 }
988
989 if (xsinfo && !xfrm_state_filter_match(xsinfo))
990 return 0;
991
992 if (n->nlmsg_type == XFRM_MSG_DELSA)
993 fprintf(fp, "Deleted ");
994 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
995 fprintf(fp, "Updated ");
996 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
997 fprintf(fp, "Expired ");
998
999 if (n->nlmsg_type == XFRM_MSG_DELSA)
1000 rta = XFRMSID_RTA(xsid);
1001 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
1002 rta = XFRMEXP_RTA(xexp);
1003 else
1004 rta = XFRMS_RTA(xsinfo);
1005
1006 parse_rtattr(tb, XFRMA_MAX, rta, len);
1007
1008 if (n->nlmsg_type == XFRM_MSG_DELSA) {
1009
1010
1011 if (!tb[XFRMA_SA]) {
1012 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
1013 return -1;
1014 }
1015 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
1016 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
1017 return -1;
1018 }
1019 xsinfo = RTA_DATA(tb[XFRMA_SA]);
1020 }
1021
1022 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL, nokeys);
1023
1024 if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
1025 fprintf(fp, "\t");
1026 fprintf(fp, "hard %u", xexp->hard);
1027 fprintf(fp, "%s", _SL_);
1028 }
1029
1030 if (oneline)
1031 fprintf(fp, "\n");
1032 fflush(fp);
1033
1034 return 0;
1035}
1036
1037int xfrm_state_print(struct nlmsghdr *n, void *arg)
1038{
1039 return __do_xfrm_state_print(n, arg, false);
1040}
1041
1042int xfrm_state_print_nokeys(struct nlmsghdr *n, void *arg)
1043{
1044 return __do_xfrm_state_print(n, arg, true);
1045}
1046
1047static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
1048{
1049 struct rtnl_handle rth;
1050 struct {
1051 struct nlmsghdr n;
1052 struct xfrm_usersa_id xsid;
1053 char buf[RTA_BUF_SIZE];
1054 } req = {
1055 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid)),
1056 .n.nlmsg_flags = NLM_F_REQUEST,
1057 .n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA,
1058 .xsid.family = preferred_family,
1059 };
1060 struct xfrm_id id;
1061 char *idp = NULL;
1062 struct xfrm_mark mark = {0, 0};
1063
1064 while (argc > 0) {
1065 xfrm_address_t saddr;
1066
1067 if (strcmp(*argv, "mark") == 0) {
1068 xfrm_parse_mark(&mark, &argc, &argv);
1069 } else {
1070 if (idp)
1071 invarg("unknown", *argv);
1072 idp = *argv;
1073
1074
1075 memset(&id, 0, sizeof(id));
1076 memset(&saddr, 0, sizeof(saddr));
1077 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
1078 &argc, &argv);
1079
1080 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
1081 req.xsid.spi = id.spi;
1082 req.xsid.proto = id.proto;
1083
1084 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
1085 (void *)&saddr, sizeof(saddr));
1086 }
1087
1088 argc--; argv++;
1089 }
1090
1091 if (mark.m & mark.v) {
1092 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
1093 (void *)&mark, sizeof(mark));
1094 if (r < 0) {
1095 fprintf(stderr, "XFRMA_MARK failed\n");
1096 exit(1);
1097 }
1098 }
1099
1100 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1101 exit(1);
1102
1103 if (req.xsid.family == AF_UNSPEC)
1104 req.xsid.family = AF_INET;
1105
1106 if (delete) {
1107 if (rtnl_talk(&rth, &req.n, NULL) < 0)
1108 exit(2);
1109 } else {
1110 struct nlmsghdr *answer;
1111
1112 if (rtnl_talk(&rth, &req.n, &answer) < 0)
1113 exit(2);
1114
1115 if (xfrm_state_print(answer, (void *)stdout) < 0) {
1116 fprintf(stderr, "An error :-)\n");
1117 exit(1);
1118 }
1119
1120 free(answer);
1121 }
1122
1123 rtnl_close(&rth);
1124
1125 return 0;
1126}
1127
1128
1129
1130
1131
1132static int xfrm_state_keep(struct nlmsghdr *n, void *arg)
1133{
1134 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
1135 struct rtnl_handle *rth = xb->rth;
1136 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
1137 int len = n->nlmsg_len;
1138 struct nlmsghdr *new_n;
1139 struct xfrm_usersa_id *xsid;
1140 struct rtattr *tb[XFRMA_MAX+1];
1141
1142 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
1143 fprintf(stderr, "Not a state: %08x %08x %08x\n",
1144 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
1145 return 0;
1146 }
1147
1148 len -= NLMSG_LENGTH(sizeof(*xsinfo));
1149 if (len < 0) {
1150 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
1151 return -1;
1152 }
1153
1154 if (!xfrm_state_filter_match(xsinfo))
1155 return 0;
1156
1157 if (xsinfo->id.proto == IPPROTO_IPIP ||
1158 xsinfo->id.proto == IPPROTO_IPV6)
1159 return 0;
1160
1161 if (xb->offset > xb->size) {
1162 fprintf(stderr, "State buffer overflow\n");
1163 return -1;
1164 }
1165
1166 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
1167 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
1168 new_n->nlmsg_flags = NLM_F_REQUEST;
1169 new_n->nlmsg_type = XFRM_MSG_DELSA;
1170 new_n->nlmsg_seq = ++rth->seq;
1171
1172 xsid = NLMSG_DATA(new_n);
1173 xsid->family = xsinfo->family;
1174 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
1175 xsid->spi = xsinfo->id.spi;
1176 xsid->proto = xsinfo->id.proto;
1177
1178 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
1179 sizeof(xsid->daddr));
1180
1181 parse_rtattr(tb, XFRMA_MAX, XFRMS_RTA(xsinfo), len);
1182
1183 if (tb[XFRMA_MARK]) {
1184 int r = addattr_l(new_n, xb->size, XFRMA_MARK,
1185 (void *)RTA_DATA(tb[XFRMA_MARK]), tb[XFRMA_MARK]->rta_len);
1186 if (r < 0) {
1187 fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
1188 exit(1);
1189 }
1190 }
1191
1192 xb->offset += new_n->nlmsg_len;
1193 xb->nlmsg_count++;
1194
1195 return 0;
1196}
1197
1198static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
1199{
1200 char *idp = NULL;
1201 struct rtnl_handle rth;
1202 bool nokeys = false;
1203
1204 if (argc > 0 || preferred_family != AF_UNSPEC)
1205 filter.use = 1;
1206 filter.xsinfo.family = preferred_family;
1207
1208 while (argc > 0) {
1209 if (strcmp(*argv, "nokeys") == 0) {
1210 nokeys = true;
1211 } else if (strcmp(*argv, "mode") == 0) {
1212 NEXT_ARG();
1213 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
1214
1215 filter.mode_mask = XFRM_FILTER_MASK_FULL;
1216
1217 } else if (strcmp(*argv, "reqid") == 0) {
1218 NEXT_ARG();
1219 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
1220
1221 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
1222
1223 } else if (strcmp(*argv, "flag") == 0) {
1224 NEXT_ARG();
1225 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
1226
1227 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
1228
1229 } else {
1230 if (idp)
1231 invarg("unknown", *argv);
1232 idp = *argv;
1233
1234
1235 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
1236 &filter.xsinfo.family, 1, &argc, &argv);
1237 if (preferred_family == AF_UNSPEC)
1238 preferred_family = filter.xsinfo.family;
1239 }
1240 argc--; argv++;
1241 }
1242
1243 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1244 exit(1);
1245
1246 if (deleteall) {
1247 struct xfrm_buffer xb;
1248 char buf[NLMSG_DELETEALL_BUF_SIZE];
1249 int i;
1250
1251 xb.buf = buf;
1252 xb.size = sizeof(buf);
1253 xb.rth = &rth;
1254
1255 for (i = 0; ; i++) {
1256 struct {
1257 struct nlmsghdr n;
1258 char buf[NLMSG_BUF_SIZE];
1259 } req = {
1260 .n.nlmsg_len = NLMSG_HDRLEN,
1261 .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
1262 .n.nlmsg_type = XFRM_MSG_GETSA,
1263 .n.nlmsg_seq = rth.dump = ++rth.seq,
1264 };
1265
1266 xb.offset = 0;
1267 xb.nlmsg_count = 0;
1268
1269 if (show_stats > 1)
1270 fprintf(stderr, "Delete-all round = %d\n", i);
1271
1272 if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) {
1273 perror("Cannot send dump request");
1274 exit(1);
1275 }
1276
1277 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb) < 0) {
1278 fprintf(stderr, "Delete-all terminated\n");
1279 exit(1);
1280 }
1281 if (xb.nlmsg_count == 0) {
1282 if (show_stats > 1)
1283 fprintf(stderr, "Delete-all completed\n");
1284 break;
1285 }
1286
1287 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
1288 perror("Failed to send delete-all request\n");
1289 exit(1);
1290 }
1291 if (show_stats > 1)
1292 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
1293
1294 xb.offset = 0;
1295 xb.nlmsg_count = 0;
1296 }
1297
1298 } else {
1299 struct xfrm_address_filter addrfilter = {
1300 .saddr = filter.xsinfo.saddr,
1301 .daddr = filter.xsinfo.id.daddr,
1302 .family = filter.xsinfo.family,
1303 .splen = filter.id_src_mask,
1304 .dplen = filter.id_dst_mask,
1305 };
1306 struct {
1307 struct nlmsghdr n;
1308 char buf[NLMSG_BUF_SIZE];
1309 } req = {
1310 .n.nlmsg_len = NLMSG_HDRLEN,
1311 .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
1312 .n.nlmsg_type = XFRM_MSG_GETSA,
1313 .n.nlmsg_seq = rth.dump = ++rth.seq,
1314 };
1315
1316 if (filter.xsinfo.id.proto)
1317 addattr8(&req.n, sizeof(req), XFRMA_PROTO,
1318 filter.xsinfo.id.proto);
1319 addattr_l(&req.n, sizeof(req), XFRMA_ADDRESS_FILTER,
1320 &addrfilter, sizeof(addrfilter));
1321
1322 if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) {
1323 perror("Cannot send dump request");
1324 exit(1);
1325 }
1326
1327 rtnl_filter_t filter = nokeys ?
1328 xfrm_state_print_nokeys : xfrm_state_print;
1329 if (rtnl_dump_filter(&rth, filter, stdout) < 0) {
1330 fprintf(stderr, "Dump terminated\n");
1331 exit(1);
1332 }
1333 }
1334
1335 rtnl_close(&rth);
1336
1337 exit(0);
1338}
1339
1340static int print_sadinfo(struct nlmsghdr *n, void *arg)
1341{
1342 FILE *fp = (FILE *)arg;
1343 __u32 *f = NLMSG_DATA(n);
1344 struct rtattr *tb[XFRMA_SAD_MAX+1];
1345 struct rtattr *rta;
1346 int len = n->nlmsg_len;
1347
1348 len -= NLMSG_LENGTH(sizeof(__u32));
1349 if (len < 0) {
1350 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
1351 return -1;
1352 }
1353
1354 rta = XFRMSAPD_RTA(f);
1355 parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
1356
1357 if (tb[XFRMA_SAD_CNT]) {
1358 __u32 cnt;
1359
1360 fprintf(fp, "\t SAD");
1361 cnt = rta_getattr_u32(tb[XFRMA_SAD_CNT]);
1362 fprintf(fp, " count %u", cnt);
1363 } else {
1364 fprintf(fp, "BAD SAD info returned\n");
1365 return -1;
1366 }
1367
1368 if (show_stats) {
1369 if (tb[XFRMA_SAD_HINFO]) {
1370 struct xfrmu_sadhinfo *si;
1371
1372 if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) {
1373 fprintf(fp, "BAD SAD length returned\n");
1374 return -1;
1375 }
1376
1377 si = RTA_DATA(tb[XFRMA_SAD_HINFO]);
1378 fprintf(fp, " (buckets ");
1379 fprintf(fp, "count %d", si->sadhcnt);
1380 fprintf(fp, " Max %d", si->sadhmcnt);
1381 fprintf(fp, ")");
1382 }
1383 }
1384 fprintf(fp, "\n");
1385
1386 return 0;
1387}
1388
1389static int xfrm_sad_getinfo(int argc, char **argv)
1390{
1391 struct rtnl_handle rth;
1392 struct {
1393 struct nlmsghdr n;
1394 __u32 flags;
1395 } req = {
1396 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags)),
1397 .n.nlmsg_flags = NLM_F_REQUEST,
1398 .n.nlmsg_type = XFRM_MSG_GETSADINFO,
1399 .flags = 0XFFFFFFFF,
1400 };
1401 struct nlmsghdr *answer;
1402
1403 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1404 exit(1);
1405
1406 if (rtnl_talk(&rth, &req.n, &answer) < 0)
1407 exit(2);
1408
1409 print_sadinfo(answer, (void *)stdout);
1410
1411 free(answer);
1412 rtnl_close(&rth);
1413
1414 return 0;
1415}
1416
1417static int xfrm_state_flush(int argc, char **argv)
1418{
1419 struct rtnl_handle rth;
1420 struct {
1421 struct nlmsghdr n;
1422 struct xfrm_usersa_flush xsf;
1423 } req = {
1424 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf)),
1425 .n.nlmsg_flags = NLM_F_REQUEST,
1426 .n.nlmsg_type = XFRM_MSG_FLUSHSA,
1427 };
1428 char *protop = NULL;
1429
1430 while (argc > 0) {
1431 if (strcmp(*argv, "proto") == 0) {
1432 int ret;
1433
1434 if (protop)
1435 duparg("proto", *argv);
1436 protop = *argv;
1437
1438 NEXT_ARG();
1439
1440 ret = xfrm_xfrmproto_getbyname(*argv);
1441 if (ret < 0)
1442 invarg("XFRM-PROTO value is invalid", *argv);
1443
1444 req.xsf.proto = (__u8)ret;
1445 } else
1446 invarg("unknown", *argv);
1447
1448 argc--; argv++;
1449 }
1450
1451 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1452 exit(1);
1453
1454 if (show_stats > 1)
1455 fprintf(stderr, "Flush state with XFRM-PROTO value \"%s\"\n",
1456 strxf_xfrmproto(req.xsf.proto));
1457
1458 if (rtnl_talk(&rth, &req.n, NULL) < 0)
1459 exit(2);
1460
1461 rtnl_close(&rth);
1462
1463 return 0;
1464}
1465
1466int do_xfrm_state(int argc, char **argv)
1467{
1468 if (argc < 1)
1469 return xfrm_state_list_or_deleteall(0, NULL, 0);
1470
1471 if (matches(*argv, "add") == 0)
1472 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1473 argc-1, argv+1);
1474 if (matches(*argv, "update") == 0)
1475 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1476 argc-1, argv+1);
1477 if (matches(*argv, "allocspi") == 0)
1478 return xfrm_state_allocspi(argc-1, argv+1);
1479 if (matches(*argv, "delete") == 0)
1480 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
1481 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1482 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
1483 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1484 || matches(*argv, "lst") == 0)
1485 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
1486 if (matches(*argv, "get") == 0)
1487 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
1488 if (matches(*argv, "flush") == 0)
1489 return xfrm_state_flush(argc-1, argv+1);
1490 if (matches(*argv, "count") == 0) {
1491 return xfrm_sad_getinfo(argc, argv);
1492 }
1493 if (matches(*argv, "help") == 0)
1494 usage();
1495 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);
1496 exit(-1);
1497}
1498