]>
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 | |
46 | * ... (max count of rtattr is XFRM_MAX_DEPTH) | |
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"); | |
59 | fprintf(stderr, " [ reqid REQID ] [ FLAG-LIST ] [ sel SELECTOR ] [ LIMIT-LIST ]\n"); | |
60 | ||
61 | fprintf(stderr, "Usage: ip xfrm state { delete | get } ID\n"); | |
62 | fprintf(stderr, "Usage: ip xfrm state { flush | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n"); | |
63 | fprintf(stderr, " [ FLAG_LIST ]\n"); | |
64 | ||
65 | fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n"); | |
66 | fprintf(stderr, "XFRM_PROTO := [ esp | ah | ipcomp ]\n"); | |
67 | //fprintf(stderr, "SPI - security parameter index(default=0)\n"); | |
68 | ||
69 | fprintf(stderr, "MODE := [ transport | tunnel ](default=transport)\n"); | |
70 | //fprintf(stderr, "REQID - number(default=0)\n"); | |
71 | ||
72 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] [ flag FLAG ]\n"); | |
73 | fprintf(stderr, "FLAG := [ noecn ]\n"); | |
74 | ||
75 | fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ algo ALGO ]\n"); | |
76 | fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n"); | |
77 | fprintf(stderr, "ALGO_TYPE := [ E | A | C ]\n"); | |
78 | //fprintf(stderr, "ALGO_NAME - algorithm name\n"); | |
79 | //fprintf(stderr, "ALGO_KEY - algorithm key\n"); | |
80 | ||
81 | fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ upspec UPSPEC ] [ dev DEV ]\n"); | |
82 | ||
83 | fprintf(stderr, "UPSPEC := proto PROTO [ sport PORT ] [ dport PORT ]\n"); | |
84 | ||
85 | //fprintf(stderr, "DEV - device name(default=none)\n"); | |
86 | fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n"); | |
87 | fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n"); | |
88 | fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n"); | |
89 | exit(-1); | |
90 | } | |
91 | ||
92 | static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type, | |
93 | char *name, char *key, int max) | |
94 | { | |
95 | int len; | |
96 | ||
97 | #if 1 | |
98 | /* XXX: verifying both name and key is required! */ | |
99 | fprintf(stderr, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n"); | |
100 | #endif | |
101 | ||
102 | strncpy(alg->alg_name, name, sizeof(alg->alg_name)); | |
103 | ||
104 | if (strncmp(key, "0x", 2) == 0) { | |
105 | /* | |
106 | * XXX: fix me!! | |
107 | */ | |
108 | __u64 val = 0; | |
109 | char *p = (char *)&val; | |
110 | ||
111 | if (get_u64(&val, key, 16)) | |
112 | invarg("\"ALGOKEY\" is invalid", key); | |
113 | ||
114 | len = (strlen(key) - 2) / 2; | |
115 | if (len > sizeof(val)) | |
116 | invarg("\"ALGOKEY\" is invalid: too large", key); | |
117 | ||
118 | if (len > 0) { | |
119 | int index = sizeof(val) - len; | |
120 | if (len > max) | |
121 | invarg("\"ALGOKEY\" makes buffer overflow\n", key); | |
122 | ||
123 | memcpy(alg->alg_key, &p[index], len); | |
124 | } | |
125 | ||
126 | } else { | |
127 | len = strlen(key); | |
128 | if (len > 0) { | |
129 | if (len > max) | |
130 | invarg("\"ALGOKEY\" makes buffer overflow\n", key); | |
131 | ||
132 | strncpy(alg->alg_key, key, len); | |
133 | } | |
134 | } | |
135 | ||
136 | alg->alg_key_len = len * 8; | |
137 | ||
138 | return 0; | |
139 | } | |
140 | ||
141 | static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp) | |
142 | { | |
143 | int argc = *argcp; | |
144 | char **argv = *argvp; | |
145 | ||
146 | if (strcmp(*argv, "noecn") == 0) | |
147 | *flags |= XFRM_STATE_NOECN; | |
148 | else | |
149 | invarg("\"FLAG\" is invalid", *argv); | |
150 | ||
151 | filter.state_flags_mask = XFRM_FILTER_MASK_FULL; | |
152 | ||
153 | *argcp = argc; | |
154 | *argvp = argv; | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv) | |
160 | { | |
161 | struct rtnl_handle rth; | |
162 | struct { | |
163 | struct nlmsghdr n; | |
164 | struct xfrm_usersa_info xsinfo; | |
165 | char buf[RTA_BUF_SIZE]; | |
166 | } req; | |
167 | char *idp = NULL; | |
168 | char *ealgop = NULL; | |
169 | char *aalgop = NULL; | |
170 | char *calgop = NULL; | |
171 | ||
172 | memset(&req, 0, sizeof(req)); | |
173 | ||
174 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo)); | |
175 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; | |
176 | req.n.nlmsg_type = cmd; | |
177 | req.xsinfo.family = preferred_family; | |
178 | ||
179 | req.xsinfo.lft.soft_byte_limit = XFRM_INF; | |
180 | req.xsinfo.lft.hard_byte_limit = XFRM_INF; | |
181 | req.xsinfo.lft.soft_packet_limit = XFRM_INF; | |
182 | req.xsinfo.lft.hard_packet_limit = XFRM_INF; | |
183 | ||
184 | while (argc > 0) { | |
185 | if (strcmp(*argv, "algo") == 0) { | |
186 | struct { | |
187 | struct xfrm_algo alg; | |
188 | char buf[XFRM_ALGO_KEY_BUF_SIZE]; | |
189 | } alg; | |
190 | int len; | |
191 | enum xfrm_attr_type_t type; | |
192 | char *name; | |
193 | char *key; | |
194 | ||
195 | NEXT_ARG(); | |
196 | ||
197 | if (strcmp(*argv, "E") == 0) { | |
198 | if (ealgop) | |
199 | duparg("ALGOTYPE", *argv); | |
200 | ealgop = *argv; | |
201 | type = XFRMA_ALG_CRYPT; | |
202 | } else if (strcmp(*argv, "A") == 0) { | |
203 | if (aalgop) | |
204 | duparg("ALGOTYPE", *argv); | |
205 | aalgop = *argv; | |
206 | type = XFRMA_ALG_AUTH; | |
207 | ||
208 | } else if (strcmp(*argv, "C") == 0) { | |
209 | if (calgop) | |
210 | duparg("ALGOTYPE", *argv); | |
211 | calgop = *argv; | |
212 | type = XFRMA_ALG_COMP; | |
213 | } else | |
214 | invarg("\"ALGOTYPE\" is invalid\n", *argv); | |
215 | ||
216 | if (!NEXT_ARG_OK()) | |
217 | missarg("ALGONAME"); | |
218 | NEXT_ARG(); | |
219 | name = *argv; | |
220 | ||
221 | if (!NEXT_ARG_OK()) | |
222 | missarg("ALGOKEY"); | |
223 | NEXT_ARG(); | |
224 | key = *argv; | |
225 | ||
226 | memset(&alg, 0, sizeof(alg)); | |
227 | ||
228 | xfrm_algo_parse((void *)&alg, type, name, key, sizeof(alg.buf)); | |
229 | len = sizeof(struct xfrm_algo) + alg.alg.alg_key_len; | |
230 | ||
231 | addattr_l(&req.n, sizeof(req.buf), type, | |
232 | (void *)&alg, len); | |
233 | ||
234 | } else if (strcmp(*argv, "mode") == 0) { | |
235 | NEXT_ARG(); | |
236 | xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv); | |
237 | } else if (strcmp(*argv, "reqid") == 0) { | |
238 | NEXT_ARG(); | |
239 | xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv); | |
240 | } else if (strcmp(*argv, "flag") == 0) { | |
241 | NEXT_ARG(); | |
242 | xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv); | |
243 | } else if (strcmp(*argv, "sel") == 0) { | |
244 | NEXT_ARG(); | |
245 | xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv); | |
246 | ||
247 | } else if (strcmp(*argv, "limit") == 0) { | |
248 | NEXT_ARG(); | |
249 | xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv); | |
250 | } else { | |
251 | if (idp) | |
252 | invarg("unknown", *argv); | |
253 | idp = *argv; | |
254 | ||
255 | /* ID */ | |
256 | xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id, | |
257 | &req.xsinfo.family, &argc, &argv); | |
258 | if (preferred_family == AF_UNSPEC) | |
259 | preferred_family = req.xsinfo.family; | |
260 | } | |
261 | argc--; argv++; | |
262 | } | |
263 | ||
264 | if (!idp) { | |
265 | fprintf(stderr, "Not enough information: \"ID\" is required\n"); | |
266 | exit(1); | |
267 | } | |
268 | ||
269 | if (ealgop || aalgop || calgop) { | |
270 | if (req.xsinfo.id.proto != IPPROTO_ESP && | |
271 | req.xsinfo.id.proto != IPPROTO_AH && | |
272 | req.xsinfo.id.proto != IPPROTO_COMP) { | |
273 | fprintf(stderr, "\"ALGO\" is invalid with proto=%d\n", req.xsinfo.id.proto); | |
274 | exit(1); | |
275 | } | |
276 | } else { | |
277 | if (req.xsinfo.id.proto == IPPROTO_ESP || | |
278 | req.xsinfo.id.proto == IPPROTO_AH || | |
279 | req.xsinfo.id.proto == IPPROTO_COMP) { | |
280 | fprintf(stderr, "\"ALGO\" is required with proto=%d\n", req.xsinfo.id.proto); | |
281 | exit (1); | |
282 | } | |
283 | } | |
284 | ||
285 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
286 | exit(1); | |
287 | ||
288 | if (req.xsinfo.family == AF_UNSPEC) | |
289 | req.xsinfo.family = AF_INET; | |
290 | ||
291 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
292 | exit(2); | |
293 | ||
294 | rtnl_close(&rth); | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
299 | static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo) | |
300 | { | |
301 | if (!filter.use) | |
302 | return 1; | |
303 | ||
304 | if (filter.id_src_mask) | |
305 | if (memcmp(&xsinfo->saddr, &filter.xsinfo.saddr, | |
306 | filter.id_src_mask) != 0) | |
307 | return 0; | |
308 | if (filter.id_dst_mask) | |
309 | if (memcmp(&xsinfo->id.daddr, &filter.xsinfo.id.daddr, | |
310 | filter.id_dst_mask) != 0) | |
311 | return 0; | |
312 | if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask) | |
313 | return 0; | |
314 | if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask) | |
315 | return 0; | |
316 | if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask) | |
317 | return 0; | |
318 | if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask) | |
319 | return 0; | |
320 | if (filter.state_flags_mask) | |
321 | if ((xsinfo->flags & filter.xsinfo.flags) == 0) | |
322 | return 0; | |
323 | ||
324 | return 1; | |
325 | } | |
326 | ||
327 | int xfrm_state_print(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | |
328 | { | |
329 | FILE *fp = (FILE*)arg; | |
330 | struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n); | |
331 | int len = n->nlmsg_len; | |
332 | struct rtattr * tb[XFRMA_MAX+1]; | |
333 | int ntb; | |
334 | ||
335 | if (n->nlmsg_type != XFRM_MSG_NEWSA && | |
336 | n->nlmsg_type != XFRM_MSG_DELSA) { | |
337 | fprintf(stderr, "Not a state: %08x %08x %08x\n", | |
338 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
339 | return 0; | |
340 | } | |
341 | ||
342 | len -= NLMSG_LENGTH(sizeof(*xsinfo)); | |
343 | if (len < 0) { | |
344 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
345 | return -1; | |
346 | } | |
347 | ||
348 | if (!xfrm_state_filter_match(xsinfo)) | |
349 | return 0; | |
350 | ||
351 | memset(tb, 0, sizeof(tb)); | |
352 | ntb = parse_rtattr_byindex(tb, XFRM_MAX_DEPTH, XFRMS_RTA(xsinfo), len); | |
353 | ||
354 | if (n->nlmsg_type == XFRM_MSG_DELSA) | |
355 | fprintf(fp, "Deleted "); | |
356 | ||
357 | xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode, | |
358 | xsinfo->reqid, xsinfo->family, fp, NULL); | |
359 | ||
360 | if (show_stats > 0) { | |
361 | fprintf(fp, "\t"); | |
362 | fprintf(fp, "seq 0x%08u ", xsinfo->seq); | |
363 | fprintf(fp, "replay-window %d ", xsinfo->replay_window); | |
364 | fprintf(fp, "flags "); | |
365 | if (xsinfo->flags & XFRM_STATE_NOECN) | |
366 | fprintf(fp, "noecn "); | |
367 | fprintf(fp, "(0x%s)", strxf_flags(xsinfo->flags)); | |
368 | ||
369 | fprintf(fp, "\n"); | |
370 | } | |
371 | ||
372 | xfrm_xfrma_print(tb, ntb, xsinfo->family, fp, "\t"); | |
373 | ||
374 | if (show_stats > 0) { | |
375 | fprintf(fp, "\tsel:\n"); | |
376 | xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, "\t "); | |
377 | } | |
378 | ||
379 | if (show_stats > 0) { | |
380 | xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, "\t"); | |
381 | xfrm_stats_print(&xsinfo->stats, fp, "\t"); | |
382 | } | |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
387 | static int xfrm_state_get_or_delete(int argc, char **argv, int delete) | |
388 | { | |
389 | struct rtnl_handle rth; | |
390 | struct { | |
391 | struct nlmsghdr n; | |
392 | struct xfrm_usersa_id xsid; | |
393 | } req; | |
394 | struct xfrm_id id; | |
395 | char *idp = NULL; | |
396 | ||
397 | memset(&req, 0, sizeof(req)); | |
398 | ||
399 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid)); | |
400 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
401 | req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA; | |
402 | req.xsid.family = preferred_family; | |
403 | ||
404 | while (argc > 0) { | |
405 | /* | |
406 | * XXX: Source address is not used and ignore it to follow | |
407 | * XXX: a manner of setkey e.g. in the case of deleting/getting | |
408 | * XXX: message of IPsec SA. | |
409 | */ | |
410 | xfrm_address_t ignore_saddr; | |
411 | ||
412 | if (idp) | |
413 | invarg("unknown", *argv); | |
414 | idp = *argv; | |
415 | ||
416 | /* ID */ | |
417 | memset(&id, 0, sizeof(id)); | |
418 | xfrm_id_parse(&ignore_saddr, &id, &req.xsid.family, | |
419 | &argc, &argv); | |
420 | ||
421 | memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr)); | |
422 | req.xsid.spi = id.spi; | |
423 | req.xsid.proto = id.proto; | |
424 | ||
425 | argc--; argv++; | |
426 | } | |
427 | ||
428 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
429 | exit(1); | |
430 | ||
431 | if (req.xsid.family == AF_UNSPEC) | |
432 | req.xsid.family = AF_INET; | |
433 | ||
434 | if (delete) { | |
435 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
436 | exit(2); | |
437 | } else { | |
438 | char buf[NLMSG_BUF_SIZE]; | |
439 | struct nlmsghdr *res_n = (struct nlmsghdr *)buf; | |
440 | ||
441 | memset(buf, 0, sizeof(buf)); | |
442 | ||
443 | if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0) | |
444 | exit(2); | |
445 | ||
446 | if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) { | |
447 | fprintf(stderr, "An error :-)\n"); | |
448 | exit(1); | |
449 | } | |
450 | } | |
451 | ||
452 | rtnl_close(&rth); | |
453 | ||
454 | return 0; | |
455 | } | |
456 | ||
457 | /* | |
458 | * With an existing state of nlmsg, make new nlmsg for deleting the state | |
459 | * and store it to buffer. | |
460 | */ | |
461 | int xfrm_state_keep(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | |
462 | { | |
463 | struct xfrm_buffer *xb = (struct xfrm_buffer *)arg; | |
464 | struct rtnl_handle *rth = xb->rth; | |
465 | struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n); | |
466 | int len = n->nlmsg_len; | |
467 | struct nlmsghdr *new_n; | |
468 | struct xfrm_usersa_id *xsid; | |
469 | ||
470 | if (n->nlmsg_type != XFRM_MSG_NEWSA) { | |
471 | fprintf(stderr, "Not a state: %08x %08x %08x\n", | |
472 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
473 | return 0; | |
474 | } | |
475 | ||
476 | len -= NLMSG_LENGTH(sizeof(*xsinfo)); | |
477 | if (len < 0) { | |
478 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
479 | return -1; | |
480 | } | |
481 | ||
482 | if (!xfrm_state_filter_match(xsinfo)) | |
483 | return 0; | |
484 | ||
485 | if (xb->offset > xb->size) { | |
486 | fprintf(stderr, "Flush buffer overflow\n"); | |
487 | return -1; | |
488 | } | |
489 | ||
490 | new_n = (struct nlmsghdr *)(xb->buf + xb->offset); | |
491 | new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid)); | |
492 | new_n->nlmsg_flags = NLM_F_REQUEST; | |
493 | new_n->nlmsg_type = XFRM_MSG_DELSA; | |
494 | new_n->nlmsg_seq = ++rth->seq; | |
495 | ||
496 | xsid = NLMSG_DATA(new_n); | |
497 | xsid->family = xsinfo->family; | |
498 | memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr)); | |
499 | xsid->spi = xsinfo->id.spi; | |
500 | xsid->proto = xsinfo->id.proto; | |
501 | ||
502 | xb->offset += new_n->nlmsg_len; | |
503 | xb->nlmsg_count ++; | |
504 | ||
505 | return 0; | |
506 | } | |
507 | ||
508 | static int xfrm_state_list_or_flush(int argc, char **argv, int flush) | |
509 | { | |
510 | char *idp = NULL; | |
511 | struct rtnl_handle rth; | |
512 | ||
513 | filter.use = 1; | |
514 | filter.xsinfo.family = preferred_family; | |
515 | ||
516 | while (argc > 0) { | |
517 | if (strcmp(*argv, "mode") == 0) { | |
518 | NEXT_ARG(); | |
519 | xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv); | |
520 | ||
521 | filter.mode_mask = XFRM_FILTER_MASK_FULL; | |
522 | ||
523 | } else if (strcmp(*argv, "reqid") == 0) { | |
524 | NEXT_ARG(); | |
525 | xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv); | |
526 | ||
527 | filter.reqid_mask = XFRM_FILTER_MASK_FULL; | |
528 | ||
529 | } else if (strcmp(*argv, "flag") == 0) { | |
530 | NEXT_ARG(); | |
531 | xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv); | |
532 | ||
533 | filter.state_flags_mask = XFRM_FILTER_MASK_FULL; | |
534 | ||
535 | } else { | |
536 | if (idp) | |
537 | invarg("unknown", *argv); | |
538 | idp = *argv; | |
539 | ||
540 | /* ID */ | |
541 | xfrm_id_parse(&filter.xsinfo.saddr, | |
542 | &filter.xsinfo.id, | |
543 | &filter.xsinfo.family, &argc, &argv); | |
544 | if (preferred_family == AF_UNSPEC) | |
545 | preferred_family = filter.xsinfo.family; | |
546 | } | |
547 | argc--; argv++; | |
548 | } | |
549 | ||
550 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
551 | exit(1); | |
552 | ||
553 | if (flush) { | |
554 | struct xfrm_buffer xb; | |
555 | char buf[NLMSG_FLUSH_BUF_SIZE]; | |
556 | int i; | |
557 | ||
558 | xb.buf = buf; | |
559 | xb.size = sizeof(buf); | |
560 | xb.rth = &rth; | |
561 | ||
562 | for (i = 0; ; i++) { | |
563 | xb.offset = 0; | |
564 | xb.nlmsg_count = 0; | |
565 | ||
566 | if (show_stats > 1) | |
567 | fprintf(stderr, "Flush round = %d\n", i); | |
568 | ||
569 | if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) { | |
570 | perror("Cannot send dump request"); | |
571 | exit(1); | |
572 | } | |
573 | ||
574 | if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) { | |
575 | fprintf(stderr, "Flush terminated\n"); | |
576 | exit(1); | |
577 | } | |
578 | if (xb.nlmsg_count == 0) { | |
579 | if (show_stats > 1) | |
580 | fprintf(stderr, "Flush completed\n"); | |
581 | break; | |
582 | } | |
583 | ||
584 | if (rtnl_send(&rth, xb.buf, xb.offset) < 0) { | |
585 | perror("Failed to send flush request\n"); | |
586 | exit(1); | |
587 | } | |
588 | if (show_stats > 1) | |
589 | fprintf(stderr, "Flushed nlmsg count = %d\n", xb.nlmsg_count); | |
590 | ||
591 | xb.offset = 0; | |
592 | xb.nlmsg_count = 0; | |
593 | } | |
594 | ||
595 | } else { | |
596 | if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) { | |
597 | perror("Cannot send dump request"); | |
598 | exit(1); | |
599 | } | |
600 | ||
601 | if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) { | |
602 | fprintf(stderr, "Dump terminated\n"); | |
603 | exit(1); | |
604 | } | |
605 | } | |
606 | ||
607 | rtnl_close(&rth); | |
608 | ||
609 | exit(0); | |
610 | } | |
611 | ||
612 | int do_xfrm_state(int argc, char **argv) | |
613 | { | |
614 | if (argc < 1) | |
615 | return xfrm_state_list_or_flush(0, NULL, 0); | |
616 | ||
617 | if (matches(*argv, "add") == 0) | |
618 | return xfrm_state_modify(XFRM_MSG_NEWSA, 0, | |
619 | argc-1, argv+1); | |
620 | if (matches(*argv, "update") == 0) | |
621 | return xfrm_state_modify(XFRM_MSG_UPDSA, 0, | |
622 | argc-1, argv+1); | |
623 | if (matches(*argv, "delete") == 0 || matches(*argv, "del") == 0) | |
624 | return xfrm_state_get_or_delete(argc-1, argv+1, 1); | |
625 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 | |
626 | || matches(*argv, "lst") == 0) | |
627 | return xfrm_state_list_or_flush(argc-1, argv+1, 0); | |
628 | if (matches(*argv, "get") == 0) | |
629 | return xfrm_state_get_or_delete(argc-1, argv+1, 0); | |
630 | if (matches(*argv, "flush") == 0) | |
631 | return xfrm_state_list_or_flush(argc-1, argv+1, 1); | |
632 | if (matches(*argv, "help") == 0) | |
633 | usage(); | |
634 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv); | |
635 | exit(-1); | |
636 | } |