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