]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/xfrm_state.c
ip xfrm: support setting/printing XFRMA_IF_ID attribute in states/policies
[mirror_iproute2.git] / ip / xfrm_state.c
CommitLineData
c7699875 1/* $USAGI: $ */
2
3/*
4 * Copyright (C)2004 USAGI/WIDE Project
ae665a52 5 *
c7699875 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.
ae665a52 10 *
c7699875 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.
ae665a52 15 *
c7699875 16 * You should have received a copy of the GNU General Public License
4d98ab00 17 * along with this program; if not, see <http://www.gnu.org/licenses>.
c7699875 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>
c7699875 31#include "utils.h"
32#include "xfrm.h"
33#include "ip_common.h"
34
56f5daac 35/* #define NLMSG_DELETEALL_BUF_SIZE (4096-512) */
9bec1a43 36#define NLMSG_DELETEALL_BUF_SIZE 8192
c7699875 37
38/*
39 * Receiving buffer defines:
40 * nlmsg
41 * data = struct xfrm_usersa_info
42 * rtattr
43 * rtattr
2534613e 44 * ... (max count of rtattr is XFRM_MAX+1
c7699875 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
0c7a5945 51#define CTX_BUF_SIZE 256
c7699875 52
53static void usage(void) __attribute__((noreturn));
54
55static void usage(void)
56{
cbec0219
DW
57 fprintf(stderr, "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n");
58 fprintf(stderr, " [ mark MARK [ mask MASK ] ] [ reqid REQID ] [ seq SEQ ]\n");
59 fprintf(stderr, " [ replay-window SIZE ] [ replay-seq SEQ ] [ replay-oseq SEQ ]\n");
0151b56d 60 fprintf(stderr, " [ replay-seq-hi SEQ ] [ replay-oseq-hi SEQ ]\n");
cbec0219 61 fprintf(stderr, " [ flag FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ] [ encap ENCAP ]\n");
dc8867d0 62 fprintf(stderr, " [ coa ADDR[/PLEN] ] [ ctx CTX ] [ extra-flag EXTRA-FLAG-LIST ]\n");
cfd2e727 63 fprintf(stderr, " [ offload [dev DEV] dir DIR ]\n");
2ecb61a0 64 fprintf(stderr, " [ output-mark OUTPUT-MARK ]\n");
aed63ae1 65+ fprintf(stderr, " [ if_id IF_ID ]\n");
cbec0219
DW
66 fprintf(stderr, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ mark MARK [ mask MASK ] ]\n");
67 fprintf(stderr, " [ reqid REQID ] [ seq SEQ ] [ min SPI max SPI ]\n");
68 fprintf(stderr, "Usage: ip xfrm state { delete | get } ID [ mark MARK [ mask MASK ] ]\n");
a6af9f2e
BW
69 fprintf(stderr, "Usage: ip xfrm state deleteall [ ID ] [ mode MODE ] [ reqid REQID ]\n");
70 fprintf(stderr, " [ flag FLAG-LIST ]\n");
71 fprintf(stderr, "Usage: ip xfrm state list [ nokeys ] [ ID ] [ mode MODE ] [ reqid REQID ]\n");
c1fa2253 72 fprintf(stderr, " [ flag FLAG-LIST ]\n");
cbec0219
DW
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 := ");
29aa4dd7 77 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP));
78 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH));
7ea4f5d3
MN
79 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_COMP));
80 fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ROUTING));
cbec0219
DW
81 fprintf(stderr, "%s\n", strxf_xfrmproto(IPPROTO_DSTOPTS));
82 fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] ALGO\n");
83 fprintf(stderr, "ALGO := { ");
7809c616 84 fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT));
f3b9aa3d 85 fprintf(stderr, "%s", strxf_algotype(XFRMA_ALG_AUTH));
29665f92 86 fprintf(stderr, " } ALGO-NAME ALGO-KEYMAT |\n");
f3b9aa3d 87 fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_AUTH_TRUNC));
29665f92 88 fprintf(stderr, " ALGO-NAME ALGO-KEYMAT ALGO-TRUNC-LEN |\n");
cbec0219 89 fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_AEAD));
29665f92 90 fprintf(stderr, " ALGO-NAME ALGO-KEYMAT ALGO-ICV-LEN |\n");
f3b9aa3d
DW
91 fprintf(stderr, " %s", strxf_algotype(XFRMA_ALG_COMP));
92 fprintf(stderr, " ALGO-NAME\n");
e8740e42 93 fprintf(stderr, "MODE := transport | tunnel | beet | ro | in_trigger\n");
cbec0219 94 fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
0151b56d 95 fprintf(stderr, "FLAG := noecn | decap-dscp | nopmtudisc | wildrecv | icmp | af-unspec | align4 | esn\n");
dc8867d0
ND
96 fprintf(stderr, "EXTRA-FLAG-LIST := [ EXTRA-FLAG-LIST ] EXTRA-FLAG\n");
97 fprintf(stderr, "EXTRA-FLAG := dont-encap-dscp\n");
cbec0219
DW
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");
56f5daac 115 fprintf(stderr, "ENCAP := { espinudp | espinudp-nonike } SPORT DPORT OADDR\n");
cfd2e727 116 fprintf(stderr, "DIR := in | out\n");
c7699875 117
c7699875 118 exit(-1);
119}
120
121static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type,
1758a81f 122 char *name, char *key, char *buf, int max)
c7699875 123{
124 int len;
7809c616 125 int slen = strlen(key);
c7699875 126
eaa34ee3 127#if 0
c7699875 128 /* XXX: verifying both name and key is required! */
29665f92 129 fprintf(stderr, "warning: ALGO-NAME/ALGO-KEYMAT values will be sent to the kernel promiscuously! (verifying them isn't implemented yet)\n");
c7699875 130#endif
131
532b8874 132 strlcpy(alg->alg_name, name, sizeof(alg->alg_name));
c7699875 133
7809c616 134 if (slen > 2 && strncmp(key, "0x", 2) == 0) {
54f7328a 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.
c7699875 144 */
7809c616 145
54f7328a 146 /* calculate length of the converted values(real key) */
147 len = (plen + 1) / 2;
148 if (len > max)
29665f92 149 invarg("ALGO-KEYMAT value makes buffer overflow\n", key);
c7699875 150
56f5daac 151 for (i = -(plen % 2), j = 0; j < len; i += 2, j++) {
54f7328a 152 char vbuf[3];
737f15f6 153 __u8 val;
c7699875 154
54f7328a 155 vbuf[0] = i >= 0 ? p[i] : '0';
156 vbuf[1] = p[i + 1];
157 vbuf[2] = '\0';
c7699875 158
54f7328a 159 if (get_u8(&val, vbuf, 16))
29665f92 160 invarg("ALGO-KEYMAT value is invalid", key);
7809c616 161
1758a81f 162 buf[j] = val;
c7699875 163 }
c7699875 164 } else {
7809c616 165 len = slen;
c7699875 166 if (len > 0) {
167 if (len > max)
29665f92 168 invarg("ALGO-KEYMAT value makes buffer overflow\n", key);
c7699875 169
99500b56 170 memcpy(buf, key, len);
c7699875 171 }
172 }
173
174 alg->alg_key_len = len * 8;
175
176 return 0;
177}
178
fb7399b2 179static int xfrm_seq_parse(__u32 *seq, int *argcp, char ***argvp)
180{
181 int argc = *argcp;
182 char **argv = *argvp;
183
9f7401fa 184 if (get_be32(seq, *argv, 0))
e8740e42 185 invarg("SEQ value is invalid", *argv);
fb7399b2 186
fb7399b2 187 *argcp = argc;
188 *argvp = argv;
189
190 return 0;
191}
192
c7699875 193static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp)
194{
195 int argc = *argcp;
196 char **argv = *argvp;
9e566a46 197 int len = strlen(*argv);
198
199 if (len > 2 && strncmp(*argv, "0x", 2) == 0) {
200 __u8 val = 0;
c7699875 201
9e566a46 202 if (get_u8(&val, *argv, 16))
e8740e42 203 invarg("FLAG value is invalid", *argv);
9e566a46 204 *flags = val;
205 } else {
eaa34ee3 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;
c1fa2253
MN
211 else if (strcmp(*argv, "nopmtudisc") == 0)
212 *flags |= XFRM_STATE_NOPMTUDISC;
7ea4f5d3
MN
213 else if (strcmp(*argv, "wildrecv") == 0)
214 *flags |= XFRM_STATE_WILDRECV;
15bb82c6
AB
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;
98f5519c
ND
219 else if (strcmp(*argv, "align4") == 0)
220 *flags |= XFRM_STATE_ALIGN4;
0151b56d 221 else if (strcmp(*argv, "esn") == 0)
222 *flags |= XFRM_STATE_ESN;
eaa34ee3 223 else {
224 PREV_ARG(); /* back track */
225 break;
226 }
227
228 if (!NEXT_ARG_OK())
229 break;
230 NEXT_ARG();
231 }
9e566a46 232 }
c7699875 233
c7699875 234 *argcp = argc;
235 *argvp = argv;
236
237 return 0;
238}
239
dc8867d0
ND
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
cfd2e727
BP
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
56f5daac 291static int xfrm_state_modify(int cmd, unsigned int flags, int argc, char **argv)
c7699875 292{
293 struct rtnl_handle rth;
294 struct {
4806867a 295 struct nlmsghdr n;
c7699875 296 struct xfrm_usersa_info xsinfo;
56f5daac 297 char buf[RTA_BUF_SIZE];
d17b136f
PS
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 = {};
cfd2e727
BP
310 struct xfrm_user_offload xuo = {};
311 unsigned int ifindex = 0;
312 __u8 dir = 0;
313 bool is_offload = false;
0151b56d 314 __u32 replay_window = 0;
315 __u32 seq = 0, oseq = 0, seq_hi = 0, oseq_hi = 0;
c7699875 316 char *idp = NULL;
1758a81f 317 char *aeadop = NULL;
c7699875 318 char *ealgop = NULL;
319 char *aalgop = NULL;
320 char *calgop = NULL;
7ea4f5d3 321 char *coap = NULL;
0c7a5945 322 char *sctxp = NULL;
dc8867d0 323 __u32 extra_flags = 0;
c90cda94 324 struct xfrm_mark mark = {0, 0};
0c7a5945
JL
325 struct {
326 struct xfrm_user_sec_ctx sctx;
327 char str[CTX_BUF_SIZE];
d17b136f 328 } ctx = {};
2ecb61a0 329 __u32 output_mark = 0;
aed63ae1
EB
330 bool is_if_id_set = false;
331 __u32 if_id = 0;
c7699875 332
333 while (argc > 0) {
7809c616 334 if (strcmp(*argv, "mode") == 0) {
c7699875 335 NEXT_ARG();
336 xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv);
c90cda94
JHS
337 } else if (strcmp(*argv, "mark") == 0) {
338 xfrm_parse_mark(&mark, &argc, &argv);
c7699875 339 } else if (strcmp(*argv, "reqid") == 0) {
340 NEXT_ARG();
341 xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv);
fb7399b2 342 } else if (strcmp(*argv, "seq") == 0) {
343 NEXT_ARG();
344 xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv);
eaa34ee3 345 } else if (strcmp(*argv, "replay-window") == 0) {
346 NEXT_ARG();
0151b56d 347 if (get_u32(&replay_window, *argv, 0))
e8740e42 348 invarg("value after \"replay-window\" is invalid", *argv);
de95ae7c
HX
349 } else if (strcmp(*argv, "replay-seq") == 0) {
350 NEXT_ARG();
0151b56d 351 if (get_u32(&seq, *argv, 0))
e8740e42 352 invarg("value after \"replay-seq\" is invalid", *argv);
0151b56d 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);
de95ae7c
HX
357 } else if (strcmp(*argv, "replay-oseq") == 0) {
358 NEXT_ARG();
0151b56d 359 if (get_u32(&oseq, *argv, 0))
e8740e42 360 invarg("value after \"replay-oseq\" is invalid", *argv);
0151b56d 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);
c7699875 365 } else if (strcmp(*argv, "flag") == 0) {
366 NEXT_ARG();
367 xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv);
dc8867d0
ND
368 } else if (strcmp(*argv, "extra-flag") == 0) {
369 NEXT_ARG();
370 xfrm_state_extra_flag_parse(&extra_flags, &argc, &argv);
c7699875 371 } else if (strcmp(*argv, "sel") == 0) {
372 NEXT_ARG();
23d5b0d5 373 preferred_family = AF_UNSPEC;
c7699875 374 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
0c5982fd 375 preferred_family = req.xsinfo.sel.family;
c7699875 376 } else if (strcmp(*argv, "limit") == 0) {
377 NEXT_ARG();
378 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
5cf576d9
SH
379 } else if (strcmp(*argv, "encap") == 0) {
380 struct xfrm_encap_tmpl encap;
381 inet_prefix oa;
56f5daac 382 NEXT_ARG();
5cf576d9
SH
383 xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
384 NEXT_ARG();
9f7401fa 385 if (get_be16(&encap.encap_sport, *argv, 0))
e8740e42 386 invarg("SPORT value after \"encap\" is invalid", *argv);
5cf576d9 387 NEXT_ARG();
9f7401fa 388 if (get_be16(&encap.encap_dport, *argv, 0))
e8740e42 389 invarg("DPORT value after \"encap\" is invalid", *argv);
5cf576d9
SH
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));
7ea4f5d3
MN
395 } else if (strcmp(*argv, "coa") == 0) {
396 inet_prefix coa;
d17b136f 397 xfrm_address_t xcoa = {};
7ea4f5d3
MN
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)
e8740e42 407 invarg("value after \"coa\" has an unrecognized address family", *argv);
7ea4f5d3 408 if (coa.bytelen > sizeof(xcoa))
e8740e42 409 invarg("value after \"coa\" is too large", *argv);
7ea4f5d3 410
7ea4f5d3
MN
411 memcpy(&xcoa, &coa.data, coa.bytelen);
412
413 addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR,
414 (void *)&xcoa, sizeof(xcoa));
0c7a5945
JL
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);
cfd2e727
BP
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 }
2ecb61a0
SAK
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);
aed63ae1
EB
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;
c7699875 456 } else {
7809c616 457 /* try to assume ALGO */
458 int type = xfrm_algotype_getbyname(*argv);
56f5daac 459
7809c616 460 switch (type) {
1758a81f 461 case XFRMA_ALG_AEAD:
7809c616 462 case XFRMA_ALG_CRYPT:
463 case XFRMA_ALG_AUTH:
f323f2a3 464 case XFRMA_ALG_AUTH_TRUNC:
7809c616 465 case XFRMA_ALG_COMP:
466 {
467 /* ALGO */
468 struct {
1758a81f
HX
469 union {
470 struct xfrm_algo alg;
471 struct xfrm_algo_aead aead;
f323f2a3 472 struct xfrm_algo_auth auth;
1758a81f 473 } u;
7809c616 474 char buf[XFRM_ALGO_KEY_BUF_SIZE];
1758a81f 475 } alg = {};
7809c616 476 int len;
f323f2a3 477 __u32 icvlen, trunclen;
7809c616 478 char *name;
f3b9aa3d 479 char *key = "";
1758a81f 480 char *buf;
7809c616 481
482 switch (type) {
1758a81f 483 case XFRMA_ALG_AEAD:
ec839527 484 if (ealgop || aalgop || aeadop)
cbec0219 485 duparg("ALGO-TYPE", *argv);
1758a81f
HX
486 aeadop = *argv;
487 break;
7809c616 488 case XFRMA_ALG_CRYPT:
ec839527 489 if (ealgop || aeadop)
cbec0219 490 duparg("ALGO-TYPE", *argv);
7809c616 491 ealgop = *argv;
492 break;
493 case XFRMA_ALG_AUTH:
f323f2a3 494 case XFRMA_ALG_AUTH_TRUNC:
ec839527 495 if (aalgop || aeadop)
cbec0219 496 duparg("ALGO-TYPE", *argv);
7809c616 497 aalgop = *argv;
498 break;
499 case XFRMA_ALG_COMP:
500 if (calgop)
cbec0219 501 duparg("ALGO-TYPE", *argv);
7809c616 502 calgop = *argv;
503 break;
504 default:
505 /* not reached */
e8740e42 506 invarg("ALGO-TYPE value is invalid\n", *argv);
7809c616 507 }
508
509 if (!NEXT_ARG_OK())
cbec0219 510 missarg("ALGO-NAME");
7809c616 511 NEXT_ARG();
512 name = *argv;
513
f3b9aa3d
DW
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())
29665f92 520 missarg("ALGO-KEYMAT");
f3b9aa3d
DW
521 NEXT_ARG();
522 key = *argv;
523 break;
524 }
7809c616 525
1758a81f
HX
526 buf = alg.u.alg.alg_key;
527 len = sizeof(alg.u.alg);
528
f323f2a3
ND
529 switch (type) {
530 case XFRMA_ALG_AEAD:
531 if (!NEXT_ARG_OK())
cbec0219 532 missarg("ALGO-ICV-LEN");
f323f2a3
ND
533 NEXT_ARG();
534 if (get_u32(&icvlen, *argv, 0))
e8740e42 535 invarg("ALGO-ICV-LEN value is invalid",
f323f2a3
ND
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())
cbec0219 544 missarg("ALGO-TRUNC-LEN");
f323f2a3
ND
545 NEXT_ARG();
546 if (get_u32(&trunclen, *argv, 0))
e8740e42 547 invarg("ALGO-TRUNC-LEN value is invalid",
f323f2a3
ND
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 }
7809c616 555
556 xfrm_algo_parse((void *)&alg, type, name, key,
1758a81f 557 buf, sizeof(alg.buf));
4c0939a2 558 len += alg.u.alg.alg_key_len / 8;
7809c616 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 }
c7699875 576 }
577 argc--; argv++;
578 }
579
0151b56d 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
cfd2e727
BP
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 }
0151b56d 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 }
de95ae7c 619
dc8867d0
ND
620 if (extra_flags)
621 addattr32(&req.n, sizeof(req.buf), XFRMA_SA_EXTRA_FLAGS,
622 extra_flags);
623
c7699875 624 if (!idp) {
e8740e42 625 fprintf(stderr, "Not enough information: ID is required\n");
c7699875 626 exit(1);
627 }
628
b5574165 629 if (mark.m) {
c90cda94
JHS
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
aed63ae1
EB
638 if (is_if_id_set)
639 addattr32(&req.n, sizeof(req.buf), XFRMA_IF_ID, if_id);
640
6128fdfd
DW
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",
7ea4f5d3
MN
651 strxf_xfrmproto(req.xsinfo.id.proto));
652 exit(1);
653 }
6128fdfd
DW
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",
7ea4f5d3
MN
709 strxf_xfrmproto(req.xsinfo.id.proto));
710 exit(1);
711 }
7ea4f5d3
MN
712 }
713
6128fdfd
DW
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",
7ea4f5d3
MN
721 strxf_xfrmproto(req.xsinfo.id.proto));
722 exit(1);
6128fdfd
DW
723 default:
724 fprintf(stderr, "MODE value is invalid with XFRM-PROTO value \"%s\"\n",
7ea4f5d3 725 strxf_xfrmproto(req.xsinfo.id.proto));
6128fdfd 726 exit(1);
7ea4f5d3 727 }
7ea4f5d3 728
6128fdfd
DW
729 if (!coap) {
730 fprintf(stderr, "\"coa\" is required with XFRM-PROTO value \"%s\"\n",
7ea4f5d3 731 strxf_xfrmproto(req.xsinfo.id.proto));
c7699875 732 exit(1);
733 }
734 } else {
6128fdfd
DW
735 if (coap) {
736 fprintf(stderr, "\"coa\" is invalid with XFRM-PROTO value \"%s\"\n",
7ea4f5d3 737 strxf_xfrmproto(req.xsinfo.id.proto));
6128fdfd 738 exit(1);
c7699875 739 }
740 }
741
2ecb61a0
SAK
742 if (output_mark)
743 addattr32(&req.n, sizeof(req.buf), XFRMA_OUTPUT_MARK, output_mark);
744
c7699875 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
86bf43c7 751 if (rtnl_talk(&rth, &req.n, NULL) < 0)
c7699875 752 exit(2);
753
754 rtnl_close(&rth);
755
756 return 0;
757}
758
fb7399b2 759static int xfrm_state_allocspi(int argc, char **argv)
760{
761 struct rtnl_handle rth;
762 struct {
4806867a 763 struct nlmsghdr n;
fb7399b2 764 struct xfrm_userspi_info xspi;
56f5daac 765 char buf[RTA_BUF_SIZE];
d17b136f
PS
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 };
fb7399b2 778 char *idp = NULL;
779 char *minp = NULL;
780 char *maxp = NULL;
c90cda94 781 struct xfrm_mark mark = {0, 0};
86bf43c7 782 struct nlmsghdr *answer;
fb7399b2 783
fb7399b2 784 while (argc > 0) {
785 if (strcmp(*argv, "mode") == 0) {
786 NEXT_ARG();
787 xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
c90cda94
JHS
788 } else if (strcmp(*argv, "mark") == 0) {
789 xfrm_parse_mark(&mark, &argc, &argv);
fb7399b2 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))
e8740e42 804 invarg("value after \"min\" is invalid", *argv);
fb7399b2 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))
e8740e42 813 invarg("value after \"max\" is invalid", *argv);
fb7399b2 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) {
e8740e42 824 fprintf(stderr, "\"spi\" is invalid\n");
fb7399b2 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) {
e8740e42 834 fprintf(stderr, "Not enough information: ID is required\n");
fb7399b2 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) {
e8740e42 844 fprintf(stderr, "value after \"min\" is larger than value after \"max\"\n");
fb7399b2 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
c90cda94
JHS
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
fb7399b2 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
86bf43c7 882 if (rtnl_talk(&rth, &req.n, &answer) < 0)
fb7399b2 883 exit(2);
884
cd554f2c 885 if (xfrm_state_print(answer, (void *)stdout) < 0) {
fb7399b2 886 fprintf(stderr, "An error :-)\n");
887 exit(1);
888 }
889
86bf43c7 890 free(answer);
fb7399b2 891 rtnl_close(&rth);
892
893 return 0;
894}
895
c7699875 896static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
897{
898 if (!filter.use)
899 return 1;
900
901 if (filter.id_src_mask)
eaa34ee3 902 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
903 filter.id_src_mask))
c7699875 904 return 0;
905 if (filter.id_dst_mask)
eaa34ee3 906 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
907 filter.id_dst_mask))
c7699875 908 return 0;
909 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
910 return 0;
911 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
912 return 0;
913 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
914 return 0;
915 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
916 return 0;
917 if (filter.state_flags_mask)
918 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
919 return 0;
920
921 return 1;
922}
923
a6af9f2e 924static int __do_xfrm_state_print(struct nlmsghdr *n, void *arg, bool nokeys)
c7699875 925{
56f5daac
SH
926 FILE *fp = (FILE *)arg;
927 struct rtattr *tb[XFRMA_MAX+1];
928 struct rtattr *rta;
c595c790
SH
929 struct xfrm_usersa_info *xsinfo = NULL;
930 struct xfrm_user_expire *xexp = NULL;
931 struct xfrm_usersa_id *xsid = NULL;
932 int len = n->nlmsg_len;
c7699875 933
934 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
90f93024 935 n->nlmsg_type != XFRM_MSG_DELSA &&
669ae748 936 n->nlmsg_type != XFRM_MSG_UPDSA &&
90f93024 937 n->nlmsg_type != XFRM_MSG_EXPIRE) {
c7699875 938 fprintf(stderr, "Not a state: %08x %08x %08x\n",
939 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
940 return 0;
941 }
942
669ae748 943 if (n->nlmsg_type == XFRM_MSG_DELSA) {
94b0c901 944 /* Don't blame me for this .. Herbert made me do it */
669ae748 945 xsid = NLMSG_DATA(n);
af1b6a41 946 len -= NLMSG_SPACE(sizeof(*xsid));
669ae748 947 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
90f93024
SH
948 xexp = NLMSG_DATA(n);
949 xsinfo = &xexp->state;
af1b6a41 950 len -= NLMSG_SPACE(sizeof(*xexp));
90f93024
SH
951 } else {
952 xexp = NULL;
953 xsinfo = NLMSG_DATA(n);
af1b6a41 954 len -= NLMSG_SPACE(sizeof(*xsinfo));
90f93024
SH
955 }
956
c7699875 957 if (len < 0) {
958 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
959 return -1;
960 }
961
669ae748 962 if (xsinfo && !xfrm_state_filter_match(xsinfo))
c7699875 963 return 0;
964
669ae748 965 if (n->nlmsg_type == XFRM_MSG_DELSA)
c595c790 966 fprintf(fp, "Deleted ");
669ae748
SH
967 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
968 fprintf(fp, "Updated ");
c595c790
SH
969 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
970 fprintf(fp, "Expired ");
971
669ae748
SH
972 if (n->nlmsg_type == XFRM_MSG_DELSA)
973 rta = XFRMSID_RTA(xsid);
974 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
90f93024 975 rta = XFRMEXP_RTA(xexp);
ae665a52 976 else
90f93024
SH
977 rta = XFRMS_RTA(xsinfo);
978
979 parse_rtattr(tb, XFRMA_MAX, rta, len);
c7699875 980
c595c790 981 if (n->nlmsg_type == XFRM_MSG_DELSA) {
56f5daac 982 /* xfrm_policy_id_print(); */
669ae748
SH
983
984 if (!tb[XFRMA_SA]) {
985 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
986 return -1;
987 }
988 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
989 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
990 return -1;
c595c790 991 }
bdf9e86d 992 xsinfo = RTA_DATA(tb[XFRMA_SA]);
c595c790 993 }
c7699875 994
a6af9f2e 995 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL, nokeys);
c7699875 996
90f93024
SH
997 if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
998 fprintf(fp, "\t");
999 fprintf(fp, "hard %u", xexp->hard);
1000 fprintf(fp, "%s", _SL_);
1001 }
1002
7809c616 1003 if (oneline)
1004 fprintf(fp, "\n");
669ae748 1005 fflush(fp);
7809c616 1006
c7699875 1007 return 0;
1008}
1009
a6af9f2e
BW
1010int xfrm_state_print(struct nlmsghdr *n, void *arg)
1011{
1012 return __do_xfrm_state_print(n, arg, false);
1013}
1014
1015int xfrm_state_print_nokeys(struct nlmsghdr *n, void *arg)
1016{
1017 return __do_xfrm_state_print(n, arg, true);
1018}
1019
c7699875 1020static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
1021{
1022 struct rtnl_handle rth;
1023 struct {
4806867a 1024 struct nlmsghdr n;
c7699875 1025 struct xfrm_usersa_id xsid;
56f5daac 1026 char buf[RTA_BUF_SIZE];
d17b136f
PS
1027 } req = {
1028 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid)),
1029 .n.nlmsg_flags = NLM_F_REQUEST,
1030 .n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA,
1031 .xsid.family = preferred_family,
1032 };
c7699875 1033 struct xfrm_id id;
1034 char *idp = NULL;
c90cda94 1035 struct xfrm_mark mark = {0, 0};
c7699875 1036
c7699875 1037 while (argc > 0) {
7ea4f5d3 1038 xfrm_address_t saddr;
c7699875 1039
c90cda94
JHS
1040 if (strcmp(*argv, "mark") == 0) {
1041 xfrm_parse_mark(&mark, &argc, &argv);
1042 } else {
1043 if (idp)
1044 invarg("unknown", *argv);
1045 idp = *argv;
c7699875 1046
c90cda94
JHS
1047 /* ID */
1048 memset(&id, 0, sizeof(id));
1049 memset(&saddr, 0, sizeof(saddr));
1050 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
1051 &argc, &argv);
c7699875 1052
c90cda94
JHS
1053 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
1054 req.xsid.spi = id.spi;
1055 req.xsid.proto = id.proto;
c7699875 1056
c90cda94
JHS
1057 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
1058 (void *)&saddr, sizeof(saddr));
1059 }
7ea4f5d3 1060
c7699875 1061 argc--; argv++;
1062 }
1063
c90cda94
JHS
1064 if (mark.m & mark.v) {
1065 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
1066 (void *)&mark, sizeof(mark));
1067 if (r < 0) {
1068 fprintf(stderr, "XFRMA_MARK failed\n");
1069 exit(1);
1070 }
1071 }
1072
c7699875 1073 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1074 exit(1);
1075
1076 if (req.xsid.family == AF_UNSPEC)
1077 req.xsid.family = AF_INET;
1078
1079 if (delete) {
86bf43c7 1080 if (rtnl_talk(&rth, &req.n, NULL) < 0)
c7699875 1081 exit(2);
1082 } else {
86bf43c7 1083 struct nlmsghdr *answer;
c7699875 1084
86bf43c7 1085 if (rtnl_talk(&rth, &req.n, &answer) < 0)
c7699875 1086 exit(2);
1087
cd554f2c 1088 if (xfrm_state_print(answer, (void *)stdout) < 0) {
c7699875 1089 fprintf(stderr, "An error :-)\n");
1090 exit(1);
1091 }
86bf43c7
HL
1092
1093 free(answer);
c7699875 1094 }
1095
1096 rtnl_close(&rth);
1097
1098 return 0;
1099}
1100
1101/*
1102 * With an existing state of nlmsg, make new nlmsg for deleting the state
1103 * and store it to buffer.
1104 */
cd554f2c 1105static int xfrm_state_keep(struct nlmsghdr *n, void *arg)
c7699875 1106{
1107 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
1108 struct rtnl_handle *rth = xb->rth;
1109 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
1110 int len = n->nlmsg_len;
1111 struct nlmsghdr *new_n;
1112 struct xfrm_usersa_id *xsid;
0c7d651b 1113 struct rtattr *tb[XFRMA_MAX+1];
c7699875 1114
1115 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
1116 fprintf(stderr, "Not a state: %08x %08x %08x\n",
1117 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
1118 return 0;
1119 }
1120
1121 len -= NLMSG_LENGTH(sizeof(*xsinfo));
1122 if (len < 0) {
1123 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
1124 return -1;
1125 }
1126
1127 if (!xfrm_state_filter_match(xsinfo))
1128 return 0;
1129
1130 if (xb->offset > xb->size) {
9bec1a43 1131 fprintf(stderr, "State buffer overflow\n");
c7699875 1132 return -1;
1133 }
1134
1135 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
1136 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
1137 new_n->nlmsg_flags = NLM_F_REQUEST;
1138 new_n->nlmsg_type = XFRM_MSG_DELSA;
1139 new_n->nlmsg_seq = ++rth->seq;
1140
1141 xsid = NLMSG_DATA(new_n);
1142 xsid->family = xsinfo->family;
1143 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
1144 xsid->spi = xsinfo->id.spi;
1145 xsid->proto = xsinfo->id.proto;
1146
7ea4f5d3
MN
1147 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
1148 sizeof(xsid->daddr));
1149
0c7d651b
TE
1150 parse_rtattr(tb, XFRMA_MAX, XFRMS_RTA(xsinfo), len);
1151
1152 if (tb[XFRMA_MARK]) {
1153 int r = addattr_l(new_n, xb->size, XFRMA_MARK,
1154 (void *)RTA_DATA(tb[XFRMA_MARK]), tb[XFRMA_MARK]->rta_len);
1155 if (r < 0) {
1156 fprintf(stderr, "%s: XFRMA_MARK failed\n", __func__);
1157 exit(1);
1158 }
1159 }
1160
c7699875 1161 xb->offset += new_n->nlmsg_len;
56f5daac 1162 xb->nlmsg_count++;
c7699875 1163
1164 return 0;
1165}
1166
9bec1a43 1167static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
c7699875 1168{
1169 char *idp = NULL;
1170 struct rtnl_handle rth;
a6af9f2e 1171 bool nokeys = false;
c7699875 1172
56f5daac 1173 if (argc > 0)
bd641cd6 1174 filter.use = 1;
c7699875 1175 filter.xsinfo.family = preferred_family;
1176
1177 while (argc > 0) {
a6af9f2e
BW
1178 if (strcmp(*argv, "nokeys") == 0) {
1179 nokeys = true;
1180 } else if (strcmp(*argv, "mode") == 0) {
c7699875 1181 NEXT_ARG();
1182 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
1183
1184 filter.mode_mask = XFRM_FILTER_MASK_FULL;
1185
1186 } else if (strcmp(*argv, "reqid") == 0) {
1187 NEXT_ARG();
1188 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
1189
1190 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
1191
1192 } else if (strcmp(*argv, "flag") == 0) {
1193 NEXT_ARG();
1194 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
1195
1196 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
1197
1198 } else {
1199 if (idp)
1200 invarg("unknown", *argv);
1201 idp = *argv;
1202
1203 /* ID */
7809c616 1204 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
1205 &filter.xsinfo.family, 1, &argc, &argv);
c7699875 1206 if (preferred_family == AF_UNSPEC)
1207 preferred_family = filter.xsinfo.family;
1208 }
1209 argc--; argv++;
1210 }
1211
1212 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1213 exit(1);
1214
9bec1a43 1215 if (deleteall) {
c7699875 1216 struct xfrm_buffer xb;
9bec1a43 1217 char buf[NLMSG_DELETEALL_BUF_SIZE];
c7699875 1218 int i;
1219
1220 xb.buf = buf;
1221 xb.size = sizeof(buf);
1222 xb.rth = &rth;
1223
1224 for (i = 0; ; i++) {
782cf01d
ND
1225 struct {
1226 struct nlmsghdr n;
1227 char buf[NLMSG_BUF_SIZE];
1228 } req = {
1229 .n.nlmsg_len = NLMSG_HDRLEN,
1230 .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
1231 .n.nlmsg_type = XFRM_MSG_GETSA,
1232 .n.nlmsg_seq = rth.dump = ++rth.seq,
1233 };
1234
c7699875 1235 xb.offset = 0;
1236 xb.nlmsg_count = 0;
1237
1238 if (show_stats > 1)
9bec1a43 1239 fprintf(stderr, "Delete-all round = %d\n", i);
c7699875 1240
782cf01d 1241 if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) {
c7699875 1242 perror("Cannot send dump request");
1243 exit(1);
1244 }
1245
cd70f3f5 1246 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb) < 0) {
9bec1a43 1247 fprintf(stderr, "Delete-all terminated\n");
c7699875 1248 exit(1);
1249 }
1250 if (xb.nlmsg_count == 0) {
1251 if (show_stats > 1)
9bec1a43 1252 fprintf(stderr, "Delete-all completed\n");
c7699875 1253 break;
1254 }
1255
f31a37f7 1256 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
9bec1a43 1257 perror("Failed to send delete-all request\n");
c7699875 1258 exit(1);
1259 }
1260 if (show_stats > 1)
9bec1a43 1261 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
c7699875 1262
1263 xb.offset = 0;
1264 xb.nlmsg_count = 0;
1265 }
1266
1267 } else {
f687d73c
ND
1268 struct xfrm_address_filter addrfilter = {
1269 .saddr = filter.xsinfo.saddr,
1270 .daddr = filter.xsinfo.id.daddr,
1271 .family = filter.xsinfo.family,
1272 .splen = filter.id_src_mask,
1273 .dplen = filter.id_dst_mask,
1274 };
1275 struct {
1276 struct nlmsghdr n;
1277 char buf[NLMSG_BUF_SIZE];
1278 } req = {
1279 .n.nlmsg_len = NLMSG_HDRLEN,
1280 .n.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
1281 .n.nlmsg_type = XFRM_MSG_GETSA,
1282 .n.nlmsg_seq = rth.dump = ++rth.seq,
1283 };
1284
1285 if (filter.xsinfo.id.proto)
1286 addattr8(&req.n, sizeof(req), XFRMA_PROTO,
1287 filter.xsinfo.id.proto);
1288 addattr_l(&req.n, sizeof(req), XFRMA_ADDRESS_FILTER,
1289 &addrfilter, sizeof(addrfilter));
1290
1291 if (rtnl_send(&rth, (void *)&req, req.n.nlmsg_len) < 0) {
c7699875 1292 perror("Cannot send dump request");
1293 exit(1);
1294 }
1295
a6af9f2e
BW
1296 rtnl_filter_t filter = nokeys ?
1297 xfrm_state_print_nokeys : xfrm_state_print;
1298 if (rtnl_dump_filter(&rth, filter, stdout) < 0) {
c7699875 1299 fprintf(stderr, "Dump terminated\n");
1300 exit(1);
1301 }
1302 }
1303
1304 rtnl_close(&rth);
1305
1306 exit(0);
1307}
1308
d1f28cf1 1309static int print_sadinfo(struct nlmsghdr *n, void *arg)
0bb4a4c2 1310{
56f5daac 1311 FILE *fp = (FILE *)arg;
0bb4a4c2 1312 __u32 *f = NLMSG_DATA(n);
1313 struct rtattr *tb[XFRMA_SAD_MAX+1];
1314 struct rtattr *rta;
0bb4a4c2 1315 int len = n->nlmsg_len;
1316
1317 len -= NLMSG_LENGTH(sizeof(__u32));
1318 if (len < 0) {
1319 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
1320 return -1;
1321 }
1322
1323 rta = XFRMSAPD_RTA(f);
1324 parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
1325
bdf9e86d 1326 if (tb[XFRMA_SAD_CNT]) {
9f1370c0
SH
1327 __u32 cnt;
1328
56f5daac 1329 fprintf(fp, "\t SAD");
9f1370c0
SH
1330 cnt = rta_getattr_u32(tb[XFRMA_SAD_CNT]);
1331 fprintf(fp, " count %u", cnt);
0bb4a4c2 1332 } else {
56f5daac 1333 fprintf(fp, "BAD SAD info returned\n");
0bb4a4c2 1334 return -1;
1335 }
1336
1337 if (show_stats) {
bdf9e86d
SH
1338 if (tb[XFRMA_SAD_HINFO]) {
1339 struct xfrmu_sadhinfo *si;
0bb4a4c2 1340
bdf9e86d 1341 if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) {
56f5daac 1342 fprintf(fp, "BAD SAD length returned\n");
bdf9e86d
SH
1343 return -1;
1344 }
0612519e 1345
bdf9e86d 1346 si = RTA_DATA(tb[XFRMA_SAD_HINFO]);
56f5daac
SH
1347 fprintf(fp, " (buckets ");
1348 fprintf(fp, "count %d", si->sadhcnt);
1349 fprintf(fp, " Max %d", si->sadhmcnt);
1350 fprintf(fp, ")");
bdf9e86d 1351 }
0bb4a4c2 1352 }
56f5daac 1353 fprintf(fp, "\n");
0bb4a4c2 1354
56f5daac 1355 return 0;
0bb4a4c2 1356}
1357
1358static int xfrm_sad_getinfo(int argc, char **argv)
1359{
1360 struct rtnl_handle rth;
1361 struct {
1362 struct nlmsghdr n;
1363 __u32 flags;
d17b136f
PS
1364 } req = {
1365 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags)),
1366 .n.nlmsg_flags = NLM_F_REQUEST,
1367 .n.nlmsg_type = XFRM_MSG_GETSADINFO,
1368 .flags = 0XFFFFFFFF,
1369 };
86bf43c7 1370 struct nlmsghdr *answer;
0bb4a4c2 1371
1372 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1373 exit(1);
1374
86bf43c7 1375 if (rtnl_talk(&rth, &req.n, &answer) < 0)
0bb4a4c2 1376 exit(2);
1377
86bf43c7 1378 print_sadinfo(answer, (void *)stdout);
0bb4a4c2 1379
86bf43c7 1380 free(answer);
0bb4a4c2 1381 rtnl_close(&rth);
1382
1383 return 0;
1384}
1385
9bec1a43 1386static int xfrm_state_flush(int argc, char **argv)
bd641cd6 1387{
1388 struct rtnl_handle rth;
1389 struct {
1390 struct nlmsghdr n;
1391 struct xfrm_usersa_flush xsf;
d17b136f
PS
1392 } req = {
1393 .n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf)),
1394 .n.nlmsg_flags = NLM_F_REQUEST,
1395 .n.nlmsg_type = XFRM_MSG_FLUSHSA,
1396 };
9bec1a43 1397 char *protop = NULL;
bd641cd6 1398
9bec1a43
SH
1399 while (argc > 0) {
1400 if (strcmp(*argv, "proto") == 0) {
1401 int ret;
1402
1403 if (protop)
1404 duparg("proto", *argv);
1405 protop = *argv;
1406
1407 NEXT_ARG();
1408
1409 ret = xfrm_xfrmproto_getbyname(*argv);
1410 if (ret < 0)
e8740e42 1411 invarg("XFRM-PROTO value is invalid", *argv);
9bec1a43
SH
1412
1413 req.xsf.proto = (__u8)ret;
1414 } else
1415 invarg("unknown", *argv);
1416
1417 argc--; argv++;
1418 }
1419
bd641cd6 1420 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1421 exit(1);
1422
1423 if (show_stats > 1)
e8740e42 1424 fprintf(stderr, "Flush state with XFRM-PROTO value \"%s\"\n",
9bec1a43 1425 strxf_xfrmproto(req.xsf.proto));
bd641cd6 1426
86bf43c7 1427 if (rtnl_talk(&rth, &req.n, NULL) < 0)
bd641cd6 1428 exit(2);
1429
1430 rtnl_close(&rth);
1431
1432 return 0;
1433}
1434
c7699875 1435int do_xfrm_state(int argc, char **argv)
1436{
1437 if (argc < 1)
9bec1a43 1438 return xfrm_state_list_or_deleteall(0, NULL, 0);
c7699875 1439
1440 if (matches(*argv, "add") == 0)
1441 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1442 argc-1, argv+1);
1443 if (matches(*argv, "update") == 0)
1444 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1445 argc-1, argv+1);
fb7399b2 1446 if (matches(*argv, "allocspi") == 0)
1447 return xfrm_state_allocspi(argc-1, argv+1);
9bec1a43 1448 if (matches(*argv, "delete") == 0)
c7699875 1449 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
9bec1a43
SH
1450 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1451 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
c7699875 1452 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1453 || matches(*argv, "lst") == 0)
9bec1a43 1454 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
c7699875 1455 if (matches(*argv, "get") == 0)
1456 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
9bec1a43
SH
1457 if (matches(*argv, "flush") == 0)
1458 return xfrm_state_flush(argc-1, argv+1);
0bb4a4c2 1459 if (matches(*argv, "count") == 0) {
1460 return xfrm_sad_getinfo(argc, argv);
1461 }
c7699875 1462 if (matches(*argv, "help") == 0)
1463 usage();
1464 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);
1465 exit(-1);
1466}