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