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