]>
Commit | Line | Data |
---|---|---|
c7699875 | 1 | /* $USAGI: $ */ |
2 | ||
3 | /* | |
4 | * Copyright (C)2004 USAGI/WIDE Project | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | /* | |
21 | * based on iproute.c | |
22 | */ | |
23 | /* | |
24 | * Authors: | |
25 | * Masahide NAKAMURA @USAGI | |
26 | */ | |
27 | ||
28 | #include <stdio.h> | |
29 | #include <stdlib.h> | |
30 | #include <string.h> | |
31 | #include <netdb.h> | |
32 | #include <linux/xfrm.h> | |
33 | #include "utils.h" | |
34 | #include "xfrm.h" | |
35 | #include "ip_common.h" | |
36 | ||
37 | //#define NLMSG_FLUSH_BUF_SIZE (4096-512) | |
38 | #define NLMSG_FLUSH_BUF_SIZE 8192 | |
39 | ||
40 | /* | |
41 | * Receiving buffer defines: | |
42 | * nlmsg | |
43 | * data = struct xfrm_usersa_info | |
44 | * rtattr | |
45 | * rtattr | |
2534613e | 46 | * ... (max count of rtattr is XFRM_MAX+1 |
c7699875 | 47 | * |
48 | * each rtattr data = struct xfrm_algo(dynamic size) or xfrm_address_t | |
49 | */ | |
50 | #define NLMSG_BUF_SIZE 4096 | |
51 | #define RTA_BUF_SIZE 2048 | |
52 | #define XFRM_ALGO_KEY_BUF_SIZE 512 | |
53 | ||
54 | static void usage(void) __attribute__((noreturn)); | |
55 | ||
56 | static void usage(void) | |
57 | { | |
58 | fprintf(stderr, "Usage: ip xfrm state { add | update } ID [ ALGO-LIST ] [ mode MODE ]\n"); | |
eaa34ee3 | 59 | fprintf(stderr, " [ reqid REQID ] [ replay-window SIZE ] [ flag FLAG-LIST ]\n"); |
60 | fprintf(stderr, " [ sel SELECTOR ] [ LIMIT-LIST ]\n"); | |
c7699875 | 61 | |
62 | fprintf(stderr, "Usage: ip xfrm state { delete | get } ID\n"); | |
63 | fprintf(stderr, "Usage: ip xfrm state { flush | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n"); | |
eaa34ee3 | 64 | fprintf(stderr, " [ flag FLAG_LIST ]\n"); |
c7699875 | 65 | |
66 | fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n"); | |
29aa4dd7 | 67 | //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n"); |
9e566a46 | 68 | fprintf(stderr, "XFRM_PROTO := [ "); |
29aa4dd7 | 69 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP)); |
70 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH)); | |
71 | fprintf(stderr, "%s ", strxf_xfrmproto(IPPROTO_COMP)); | |
7809c616 | 72 | fprintf(stderr, "]\n"); |
9e566a46 | 73 | |
c7699875 | 74 | //fprintf(stderr, "SPI - security parameter index(default=0)\n"); |
75 | ||
76 | fprintf(stderr, "MODE := [ transport | tunnel ](default=transport)\n"); | |
77 | //fprintf(stderr, "REQID - number(default=0)\n"); | |
78 | ||
eaa34ee3 | 79 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); |
80 | fprintf(stderr, "FLAG := [ noecn | decap-dscp ]\n"); | |
c7699875 | 81 | |
7809c616 | 82 | fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n"); |
c7699875 | 83 | fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n"); |
7809c616 | 84 | fprintf(stderr, "ALGO_TYPE := [ "); |
85 | fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT)); | |
86 | fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AUTH)); | |
87 | fprintf(stderr, "%s ", strxf_algotype(XFRMA_ALG_COMP)); | |
88 | fprintf(stderr, "]\n"); | |
89 | ||
c7699875 | 90 | //fprintf(stderr, "ALGO_NAME - algorithm name\n"); |
91 | //fprintf(stderr, "ALGO_KEY - algorithm key\n"); | |
92 | ||
eaa34ee3 | 93 | fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n"); |
c7699875 | 94 | |
c70b36d2 | 95 | fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n"); |
96 | fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] ]\n"); | |
97 | ||
c7699875 | 98 | |
99 | //fprintf(stderr, "DEV - device name(default=none)\n"); | |
100 | fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n"); | |
101 | fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n"); | |
102 | fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n"); | |
103 | exit(-1); | |
104 | } | |
105 | ||
106 | static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type, | |
107 | char *name, char *key, int max) | |
108 | { | |
109 | int len; | |
7809c616 | 110 | int slen = strlen(key); |
c7699875 | 111 | |
eaa34ee3 | 112 | #if 0 |
c7699875 | 113 | /* XXX: verifying both name and key is required! */ |
114 | fprintf(stderr, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n"); | |
115 | #endif | |
116 | ||
117 | strncpy(alg->alg_name, name, sizeof(alg->alg_name)); | |
118 | ||
7809c616 | 119 | if (slen > 2 && strncmp(key, "0x", 2) == 0) { |
54f7328a | 120 | /* split two chars "0x" from the top */ |
121 | char *p = key + 2; | |
122 | int plen = slen - 2; | |
123 | int i; | |
124 | int j; | |
125 | ||
126 | /* Converting hexadecimal numbered string into real key; | |
127 | * Convert each two chars into one char(value). If number | |
128 | * of the length is odd, add zero on the top for rounding. | |
c7699875 | 129 | */ |
7809c616 | 130 | |
54f7328a | 131 | /* calculate length of the converted values(real key) */ |
132 | len = (plen + 1) / 2; | |
133 | if (len > max) | |
134 | invarg("\"ALGOKEY\" makes buffer overflow\n", key); | |
c7699875 | 135 | |
54f7328a | 136 | for (i = - (plen % 2), j = 0; j < len; i += 2, j++) { |
137 | char vbuf[3]; | |
138 | char val; | |
c7699875 | 139 | |
54f7328a | 140 | vbuf[0] = i >= 0 ? p[i] : '0'; |
141 | vbuf[1] = p[i + 1]; | |
142 | vbuf[2] = '\0'; | |
c7699875 | 143 | |
54f7328a | 144 | if (get_u8(&val, vbuf, 16)) |
145 | invarg("\"ALGOKEY\" is invalid", key); | |
7809c616 | 146 | |
54f7328a | 147 | alg->alg_key[j] = val; |
c7699875 | 148 | } |
c7699875 | 149 | } else { |
7809c616 | 150 | len = slen; |
c7699875 | 151 | if (len > 0) { |
152 | if (len > max) | |
153 | invarg("\"ALGOKEY\" makes buffer overflow\n", key); | |
154 | ||
155 | strncpy(alg->alg_key, key, len); | |
156 | } | |
157 | } | |
158 | ||
159 | alg->alg_key_len = len * 8; | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp) | |
165 | { | |
166 | int argc = *argcp; | |
167 | char **argv = *argvp; | |
9e566a46 | 168 | int len = strlen(*argv); |
169 | ||
170 | if (len > 2 && strncmp(*argv, "0x", 2) == 0) { | |
171 | __u8 val = 0; | |
c7699875 | 172 | |
9e566a46 | 173 | if (get_u8(&val, *argv, 16)) |
174 | invarg("\"FLAG\" is invalid", *argv); | |
175 | *flags = val; | |
176 | } else { | |
eaa34ee3 | 177 | while (1) { |
178 | if (strcmp(*argv, "noecn") == 0) | |
179 | *flags |= XFRM_STATE_NOECN; | |
180 | else if (strcmp(*argv, "decap-dscp") == 0) | |
181 | *flags |= XFRM_STATE_DECAP_DSCP; | |
182 | else { | |
183 | PREV_ARG(); /* back track */ | |
184 | break; | |
185 | } | |
186 | ||
187 | if (!NEXT_ARG_OK()) | |
188 | break; | |
189 | NEXT_ARG(); | |
190 | } | |
9e566a46 | 191 | } |
c7699875 | 192 | |
193 | filter.state_flags_mask = XFRM_FILTER_MASK_FULL; | |
194 | ||
195 | *argcp = argc; | |
196 | *argvp = argv; | |
197 | ||
198 | return 0; | |
199 | } | |
200 | ||
201 | static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv) | |
202 | { | |
203 | struct rtnl_handle rth; | |
204 | struct { | |
205 | struct nlmsghdr n; | |
206 | struct xfrm_usersa_info xsinfo; | |
207 | char buf[RTA_BUF_SIZE]; | |
208 | } req; | |
209 | char *idp = NULL; | |
210 | char *ealgop = NULL; | |
211 | char *aalgop = NULL; | |
212 | char *calgop = NULL; | |
213 | ||
214 | memset(&req, 0, sizeof(req)); | |
215 | ||
216 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo)); | |
217 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; | |
218 | req.n.nlmsg_type = cmd; | |
219 | req.xsinfo.family = preferred_family; | |
220 | ||
221 | req.xsinfo.lft.soft_byte_limit = XFRM_INF; | |
222 | req.xsinfo.lft.hard_byte_limit = XFRM_INF; | |
223 | req.xsinfo.lft.soft_packet_limit = XFRM_INF; | |
224 | req.xsinfo.lft.hard_packet_limit = XFRM_INF; | |
225 | ||
226 | while (argc > 0) { | |
7809c616 | 227 | if (strcmp(*argv, "mode") == 0) { |
c7699875 | 228 | NEXT_ARG(); |
229 | xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv); | |
230 | } else if (strcmp(*argv, "reqid") == 0) { | |
231 | NEXT_ARG(); | |
232 | xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv); | |
eaa34ee3 | 233 | } else if (strcmp(*argv, "replay-window") == 0) { |
234 | NEXT_ARG(); | |
235 | if (get_u8(&req.xsinfo.replay_window, *argv, 0)) | |
236 | invarg("\"replay-window\" value is invalid", *argv); | |
c7699875 | 237 | } else if (strcmp(*argv, "flag") == 0) { |
238 | NEXT_ARG(); | |
239 | xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv); | |
240 | } else if (strcmp(*argv, "sel") == 0) { | |
241 | NEXT_ARG(); | |
242 | xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv); | |
c7699875 | 243 | } else if (strcmp(*argv, "limit") == 0) { |
244 | NEXT_ARG(); | |
245 | xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv); | |
246 | } else { | |
7809c616 | 247 | /* try to assume ALGO */ |
248 | int type = xfrm_algotype_getbyname(*argv); | |
249 | switch (type) { | |
250 | case XFRMA_ALG_CRYPT: | |
251 | case XFRMA_ALG_AUTH: | |
252 | case XFRMA_ALG_COMP: | |
253 | { | |
254 | /* ALGO */ | |
255 | struct { | |
256 | struct xfrm_algo alg; | |
257 | char buf[XFRM_ALGO_KEY_BUF_SIZE]; | |
258 | } alg; | |
259 | int len; | |
260 | char *name; | |
261 | char *key; | |
262 | ||
263 | switch (type) { | |
264 | case XFRMA_ALG_CRYPT: | |
265 | if (ealgop) | |
266 | duparg("ALGOTYPE", *argv); | |
267 | ealgop = *argv; | |
268 | break; | |
269 | case XFRMA_ALG_AUTH: | |
270 | if (aalgop) | |
271 | duparg("ALGOTYPE", *argv); | |
272 | aalgop = *argv; | |
273 | break; | |
274 | case XFRMA_ALG_COMP: | |
275 | if (calgop) | |
276 | duparg("ALGOTYPE", *argv); | |
277 | calgop = *argv; | |
278 | break; | |
279 | default: | |
280 | /* not reached */ | |
281 | invarg("\"ALGOTYPE\" is invalid\n", *argv); | |
282 | } | |
283 | ||
284 | if (!NEXT_ARG_OK()) | |
285 | missarg("ALGONAME"); | |
286 | NEXT_ARG(); | |
287 | name = *argv; | |
288 | ||
289 | if (!NEXT_ARG_OK()) | |
290 | missarg("ALGOKEY"); | |
291 | NEXT_ARG(); | |
292 | key = *argv; | |
293 | ||
294 | memset(&alg, 0, sizeof(alg)); | |
295 | ||
296 | xfrm_algo_parse((void *)&alg, type, name, key, | |
297 | sizeof(alg.buf)); | |
298 | len = sizeof(struct xfrm_algo) + alg.alg.alg_key_len; | |
299 | ||
300 | addattr_l(&req.n, sizeof(req.buf), type, | |
301 | (void *)&alg, len); | |
302 | break; | |
303 | } | |
304 | default: | |
305 | /* try to assume ID */ | |
306 | if (idp) | |
307 | invarg("unknown", *argv); | |
308 | idp = *argv; | |
309 | ||
310 | /* ID */ | |
311 | xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id, | |
312 | &req.xsinfo.family, 0, &argc, &argv); | |
313 | if (preferred_family == AF_UNSPEC) | |
314 | preferred_family = req.xsinfo.family; | |
315 | } | |
c7699875 | 316 | } |
317 | argc--; argv++; | |
318 | } | |
319 | ||
320 | if (!idp) { | |
321 | fprintf(stderr, "Not enough information: \"ID\" is required\n"); | |
322 | exit(1); | |
323 | } | |
324 | ||
325 | if (ealgop || aalgop || calgop) { | |
326 | if (req.xsinfo.id.proto != IPPROTO_ESP && | |
327 | req.xsinfo.id.proto != IPPROTO_AH && | |
328 | req.xsinfo.id.proto != IPPROTO_COMP) { | |
29aa4dd7 | 329 | fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n", strxf_xfrmproto(req.xsinfo.id.proto)); |
c7699875 | 330 | exit(1); |
331 | } | |
332 | } else { | |
333 | if (req.xsinfo.id.proto == IPPROTO_ESP || | |
334 | req.xsinfo.id.proto == IPPROTO_AH || | |
335 | req.xsinfo.id.proto == IPPROTO_COMP) { | |
29aa4dd7 | 336 | fprintf(stderr, "\"ALGO\" is required with proto=%s\n", strxf_xfrmproto(req.xsinfo.id.proto)); |
c7699875 | 337 | exit (1); |
338 | } | |
339 | } | |
340 | ||
341 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
342 | exit(1); | |
343 | ||
344 | if (req.xsinfo.family == AF_UNSPEC) | |
345 | req.xsinfo.family = AF_INET; | |
346 | ||
347 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
348 | exit(2); | |
349 | ||
350 | rtnl_close(&rth); | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
355 | static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo) | |
356 | { | |
357 | if (!filter.use) | |
358 | return 1; | |
359 | ||
360 | if (filter.id_src_mask) | |
eaa34ee3 | 361 | if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr, |
362 | filter.id_src_mask)) | |
c7699875 | 363 | return 0; |
364 | if (filter.id_dst_mask) | |
eaa34ee3 | 365 | if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr, |
366 | filter.id_dst_mask)) | |
c7699875 | 367 | return 0; |
368 | if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask) | |
369 | return 0; | |
370 | if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask) | |
371 | return 0; | |
372 | if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask) | |
373 | return 0; | |
374 | if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask) | |
375 | return 0; | |
376 | if (filter.state_flags_mask) | |
377 | if ((xsinfo->flags & filter.xsinfo.flags) == 0) | |
378 | return 0; | |
379 | ||
380 | return 1; | |
381 | } | |
382 | ||
7809c616 | 383 | static int xfrm_selector_iszero(struct xfrm_selector *s) |
384 | { | |
385 | struct xfrm_selector s0; | |
386 | ||
387 | memset(&s0, 0, sizeof(s0)); | |
388 | ||
389 | return (memcmp(&s0, s, sizeof(s0)) == 0); | |
390 | } | |
391 | ||
6dc9f016 | 392 | static int xfrm_state_print(const struct sockaddr_nl *who, |
50772dc5 | 393 | struct nlmsghdr *n, |
6dc9f016 | 394 | void *arg) |
c7699875 | 395 | { |
396 | FILE *fp = (FILE*)arg; | |
397 | struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n); | |
398 | int len = n->nlmsg_len; | |
399 | struct rtattr * tb[XFRMA_MAX+1]; | |
c7699875 | 400 | |
401 | if (n->nlmsg_type != XFRM_MSG_NEWSA && | |
402 | n->nlmsg_type != XFRM_MSG_DELSA) { | |
403 | fprintf(stderr, "Not a state: %08x %08x %08x\n", | |
404 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | len -= NLMSG_LENGTH(sizeof(*xsinfo)); | |
409 | if (len < 0) { | |
410 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
411 | return -1; | |
412 | } | |
413 | ||
414 | if (!xfrm_state_filter_match(xsinfo)) | |
415 | return 0; | |
416 | ||
2534613e | 417 | parse_rtattr(tb, XFRMA_MAX, XFRMS_RTA(xsinfo), len); |
c7699875 | 418 | |
419 | if (n->nlmsg_type == XFRM_MSG_DELSA) | |
420 | fprintf(fp, "Deleted "); | |
421 | ||
422 | xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode, | |
eaa34ee3 | 423 | xsinfo->reqid, xsinfo->family, 1, fp, NULL); |
c7699875 | 424 | |
9e566a46 | 425 | fprintf(fp, "\t"); |
eaa34ee3 | 426 | fprintf(fp, "replay-window %u ", xsinfo->replay_window); |
7809c616 | 427 | if (show_stats > 0) |
c7699875 | 428 | fprintf(fp, "seq 0x%08u ", xsinfo->seq); |
eaa34ee3 | 429 | if (show_stats > 0 || xsinfo->flags) { |
430 | __u8 flags = xsinfo->flags; | |
431 | ||
432 | fprintf(fp, "flag "); | |
433 | XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOECN, "noecn"); | |
434 | XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp"); | |
435 | if (flags) | |
436 | fprintf(fp, "%x", flags); | |
437 | if (show_stats > 0) | |
438 | fprintf(fp, " (0x%s)", strxf_mask8(flags)); | |
9e566a46 | 439 | } |
7809c616 | 440 | fprintf(fp, "%s", _SL_); |
c7699875 | 441 | |
2534613e | 442 | xfrm_xfrma_print(tb, xsinfo->family, fp, "\t"); |
c7699875 | 443 | |
7809c616 | 444 | if (!xfrm_selector_iszero(&xsinfo->sel)) |
445 | xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, "\tsel "); | |
c7699875 | 446 | |
447 | if (show_stats > 0) { | |
448 | xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, "\t"); | |
449 | xfrm_stats_print(&xsinfo->stats, fp, "\t"); | |
450 | } | |
451 | ||
7809c616 | 452 | if (oneline) |
453 | fprintf(fp, "\n"); | |
454 | ||
c7699875 | 455 | return 0; |
456 | } | |
457 | ||
458 | static int xfrm_state_get_or_delete(int argc, char **argv, int delete) | |
459 | { | |
460 | struct rtnl_handle rth; | |
461 | struct { | |
462 | struct nlmsghdr n; | |
463 | struct xfrm_usersa_id xsid; | |
464 | } req; | |
465 | struct xfrm_id id; | |
466 | char *idp = NULL; | |
467 | ||
468 | memset(&req, 0, sizeof(req)); | |
469 | ||
470 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid)); | |
471 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
472 | req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA; | |
473 | req.xsid.family = preferred_family; | |
474 | ||
475 | while (argc > 0) { | |
476 | /* | |
477 | * XXX: Source address is not used and ignore it to follow | |
478 | * XXX: a manner of setkey e.g. in the case of deleting/getting | |
479 | * XXX: message of IPsec SA. | |
480 | */ | |
481 | xfrm_address_t ignore_saddr; | |
482 | ||
483 | if (idp) | |
484 | invarg("unknown", *argv); | |
485 | idp = *argv; | |
486 | ||
487 | /* ID */ | |
488 | memset(&id, 0, sizeof(id)); | |
7809c616 | 489 | xfrm_id_parse(&ignore_saddr, &id, &req.xsid.family, 0, |
c7699875 | 490 | &argc, &argv); |
491 | ||
492 | memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr)); | |
493 | req.xsid.spi = id.spi; | |
494 | req.xsid.proto = id.proto; | |
495 | ||
496 | argc--; argv++; | |
497 | } | |
498 | ||
499 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
500 | exit(1); | |
501 | ||
502 | if (req.xsid.family == AF_UNSPEC) | |
503 | req.xsid.family = AF_INET; | |
504 | ||
505 | if (delete) { | |
506 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
507 | exit(2); | |
508 | } else { | |
509 | char buf[NLMSG_BUF_SIZE]; | |
510 | struct nlmsghdr *res_n = (struct nlmsghdr *)buf; | |
511 | ||
512 | memset(buf, 0, sizeof(buf)); | |
513 | ||
514 | if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0) | |
515 | exit(2); | |
516 | ||
517 | if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) { | |
518 | fprintf(stderr, "An error :-)\n"); | |
519 | exit(1); | |
520 | } | |
521 | } | |
522 | ||
523 | rtnl_close(&rth); | |
524 | ||
525 | return 0; | |
526 | } | |
527 | ||
528 | /* | |
529 | * With an existing state of nlmsg, make new nlmsg for deleting the state | |
530 | * and store it to buffer. | |
531 | */ | |
6dc9f016 | 532 | static int xfrm_state_keep(const struct sockaddr_nl *who, |
50772dc5 | 533 | struct nlmsghdr *n, |
6dc9f016 | 534 | void *arg) |
c7699875 | 535 | { |
536 | struct xfrm_buffer *xb = (struct xfrm_buffer *)arg; | |
537 | struct rtnl_handle *rth = xb->rth; | |
538 | struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n); | |
539 | int len = n->nlmsg_len; | |
540 | struct nlmsghdr *new_n; | |
541 | struct xfrm_usersa_id *xsid; | |
542 | ||
543 | if (n->nlmsg_type != XFRM_MSG_NEWSA) { | |
544 | fprintf(stderr, "Not a state: %08x %08x %08x\n", | |
545 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
546 | return 0; | |
547 | } | |
548 | ||
549 | len -= NLMSG_LENGTH(sizeof(*xsinfo)); | |
550 | if (len < 0) { | |
551 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
552 | return -1; | |
553 | } | |
554 | ||
555 | if (!xfrm_state_filter_match(xsinfo)) | |
556 | return 0; | |
557 | ||
558 | if (xb->offset > xb->size) { | |
559 | fprintf(stderr, "Flush buffer overflow\n"); | |
560 | return -1; | |
561 | } | |
562 | ||
563 | new_n = (struct nlmsghdr *)(xb->buf + xb->offset); | |
564 | new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid)); | |
565 | new_n->nlmsg_flags = NLM_F_REQUEST; | |
566 | new_n->nlmsg_type = XFRM_MSG_DELSA; | |
567 | new_n->nlmsg_seq = ++rth->seq; | |
568 | ||
569 | xsid = NLMSG_DATA(new_n); | |
570 | xsid->family = xsinfo->family; | |
571 | memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr)); | |
572 | xsid->spi = xsinfo->id.spi; | |
573 | xsid->proto = xsinfo->id.proto; | |
574 | ||
575 | xb->offset += new_n->nlmsg_len; | |
576 | xb->nlmsg_count ++; | |
577 | ||
578 | return 0; | |
579 | } | |
580 | ||
581 | static int xfrm_state_list_or_flush(int argc, char **argv, int flush) | |
582 | { | |
583 | char *idp = NULL; | |
584 | struct rtnl_handle rth; | |
585 | ||
bd641cd6 | 586 | if(argc > 0) |
587 | filter.use = 1; | |
c7699875 | 588 | filter.xsinfo.family = preferred_family; |
589 | ||
590 | while (argc > 0) { | |
591 | if (strcmp(*argv, "mode") == 0) { | |
592 | NEXT_ARG(); | |
593 | xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv); | |
594 | ||
595 | filter.mode_mask = XFRM_FILTER_MASK_FULL; | |
596 | ||
597 | } else if (strcmp(*argv, "reqid") == 0) { | |
598 | NEXT_ARG(); | |
599 | xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv); | |
600 | ||
601 | filter.reqid_mask = XFRM_FILTER_MASK_FULL; | |
602 | ||
603 | } else if (strcmp(*argv, "flag") == 0) { | |
604 | NEXT_ARG(); | |
605 | xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv); | |
606 | ||
607 | filter.state_flags_mask = XFRM_FILTER_MASK_FULL; | |
608 | ||
609 | } else { | |
610 | if (idp) | |
611 | invarg("unknown", *argv); | |
612 | idp = *argv; | |
613 | ||
614 | /* ID */ | |
7809c616 | 615 | xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id, |
616 | &filter.xsinfo.family, 1, &argc, &argv); | |
c7699875 | 617 | if (preferred_family == AF_UNSPEC) |
618 | preferred_family = filter.xsinfo.family; | |
619 | } | |
620 | argc--; argv++; | |
621 | } | |
622 | ||
623 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
624 | exit(1); | |
625 | ||
626 | if (flush) { | |
627 | struct xfrm_buffer xb; | |
628 | char buf[NLMSG_FLUSH_BUF_SIZE]; | |
629 | int i; | |
630 | ||
631 | xb.buf = buf; | |
632 | xb.size = sizeof(buf); | |
633 | xb.rth = &rth; | |
634 | ||
635 | for (i = 0; ; i++) { | |
636 | xb.offset = 0; | |
637 | xb.nlmsg_count = 0; | |
638 | ||
639 | if (show_stats > 1) | |
640 | fprintf(stderr, "Flush round = %d\n", i); | |
641 | ||
642 | if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) { | |
643 | perror("Cannot send dump request"); | |
644 | exit(1); | |
645 | } | |
646 | ||
647 | if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) { | |
648 | fprintf(stderr, "Flush terminated\n"); | |
649 | exit(1); | |
650 | } | |
651 | if (xb.nlmsg_count == 0) { | |
652 | if (show_stats > 1) | |
653 | fprintf(stderr, "Flush completed\n"); | |
654 | break; | |
655 | } | |
656 | ||
657 | if (rtnl_send(&rth, xb.buf, xb.offset) < 0) { | |
658 | perror("Failed to send flush request\n"); | |
659 | exit(1); | |
660 | } | |
661 | if (show_stats > 1) | |
662 | fprintf(stderr, "Flushed nlmsg count = %d\n", xb.nlmsg_count); | |
663 | ||
664 | xb.offset = 0; | |
665 | xb.nlmsg_count = 0; | |
666 | } | |
667 | ||
668 | } else { | |
669 | if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) { | |
670 | perror("Cannot send dump request"); | |
671 | exit(1); | |
672 | } | |
673 | ||
674 | if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) { | |
675 | fprintf(stderr, "Dump terminated\n"); | |
676 | exit(1); | |
677 | } | |
678 | } | |
679 | ||
680 | rtnl_close(&rth); | |
681 | ||
682 | exit(0); | |
683 | } | |
684 | ||
bd641cd6 | 685 | static int xfrm_state_flush_all(void) |
686 | { | |
687 | struct rtnl_handle rth; | |
688 | struct { | |
689 | struct nlmsghdr n; | |
690 | struct xfrm_usersa_flush xsf; | |
691 | } req; | |
692 | ||
693 | memset(&req, 0, sizeof(req)); | |
694 | ||
695 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf)); | |
696 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
697 | req.n.nlmsg_type = XFRM_MSG_FLUSHSA; | |
698 | req.xsf.proto = IPSEC_PROTO_ANY; | |
699 | ||
700 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
701 | exit(1); | |
702 | ||
703 | if (show_stats > 1) | |
704 | fprintf(stderr, "Flush all\n"); | |
705 | ||
706 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
707 | exit(2); | |
708 | ||
709 | rtnl_close(&rth); | |
710 | ||
711 | return 0; | |
712 | } | |
713 | ||
c7699875 | 714 | int do_xfrm_state(int argc, char **argv) |
715 | { | |
716 | if (argc < 1) | |
717 | return xfrm_state_list_or_flush(0, NULL, 0); | |
718 | ||
719 | if (matches(*argv, "add") == 0) | |
720 | return xfrm_state_modify(XFRM_MSG_NEWSA, 0, | |
721 | argc-1, argv+1); | |
722 | if (matches(*argv, "update") == 0) | |
723 | return xfrm_state_modify(XFRM_MSG_UPDSA, 0, | |
724 | argc-1, argv+1); | |
725 | if (matches(*argv, "delete") == 0 || matches(*argv, "del") == 0) | |
726 | return xfrm_state_get_or_delete(argc-1, argv+1, 1); | |
727 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 | |
728 | || matches(*argv, "lst") == 0) | |
729 | return xfrm_state_list_or_flush(argc-1, argv+1, 0); | |
730 | if (matches(*argv, "get") == 0) | |
731 | return xfrm_state_get_or_delete(argc-1, argv+1, 0); | |
bd641cd6 | 732 | if (matches(*argv, "flush") == 0) { |
733 | if (argc-1 < 1) | |
734 | return xfrm_state_flush_all(); | |
735 | else | |
736 | return xfrm_state_list_or_flush(argc-1, argv+1, 1); | |
737 | } | |
c7699875 | 738 | if (matches(*argv, "help") == 0) |
739 | usage(); | |
740 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv); | |
741 | exit(-1); | |
742 | } |