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