]>
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"); | |
fb7399b2 | 59 | fprintf(stderr, " [ reqid REQID ] [ seq SEQ ] [ replay-window SIZE ] [ flag FLAG-LIST ]\n"); |
5cf576d9 | 60 | fprintf(stderr, " [ encap ENCAP ] [ sel SELECTOR ] [ LIMIT-LIST ]\n"); |
fb7399b2 | 61 | fprintf(stderr, "Usage: ip xfrm state allocspi ID [ mode MODE ] [ reqid REQID ] [ seq SEQ ]\n"); |
62 | fprintf(stderr, " [ min SPI max SPI ]\n"); | |
c7699875 | 63 | fprintf(stderr, "Usage: ip xfrm state { delete | get } ID\n"); |
64 | fprintf(stderr, "Usage: ip xfrm state { flush | list } [ ID ] [ mode MODE ] [ reqid REQID ]\n"); | |
eaa34ee3 | 65 | fprintf(stderr, " [ flag FLAG_LIST ]\n"); |
c7699875 | 66 | |
67 | fprintf(stderr, "ID := [ src ADDR ] [ dst ADDR ] [ proto XFRM_PROTO ] [ spi SPI ]\n"); | |
29aa4dd7 | 68 | //fprintf(stderr, "XFRM_PROTO := [ esp | ah | comp ]\n"); |
9e566a46 | 69 | fprintf(stderr, "XFRM_PROTO := [ "); |
29aa4dd7 | 70 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_ESP)); |
71 | fprintf(stderr, "%s | ", strxf_xfrmproto(IPPROTO_AH)); | |
72 | fprintf(stderr, "%s ", strxf_xfrmproto(IPPROTO_COMP)); | |
7809c616 | 73 | fprintf(stderr, "]\n"); |
9e566a46 | 74 | |
c7699875 | 75 | //fprintf(stderr, "SPI - security parameter index(default=0)\n"); |
76 | ||
77 | fprintf(stderr, "MODE := [ transport | tunnel ](default=transport)\n"); | |
78 | //fprintf(stderr, "REQID - number(default=0)\n"); | |
79 | ||
eaa34ee3 | 80 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); |
81 | fprintf(stderr, "FLAG := [ noecn | decap-dscp ]\n"); | |
5cf576d9 SH |
82 | |
83 | fprintf(stderr, "ENCAP := ENCAP-TYPE SPORT DPORT OADDR\n"); | |
84 | fprintf(stderr, "ENCAP-TYPE := espinudp | espinudp-nonike\n"); | |
c7699875 | 85 | |
7809c616 | 86 | fprintf(stderr, "ALGO-LIST := [ ALGO-LIST ] | [ ALGO ]\n"); |
c7699875 | 87 | fprintf(stderr, "ALGO := ALGO_TYPE ALGO_NAME ALGO_KEY\n"); |
7809c616 | 88 | fprintf(stderr, "ALGO_TYPE := [ "); |
89 | fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_CRYPT)); | |
90 | fprintf(stderr, "%s | ", strxf_algotype(XFRMA_ALG_AUTH)); | |
91 | fprintf(stderr, "%s ", strxf_algotype(XFRMA_ALG_COMP)); | |
92 | fprintf(stderr, "]\n"); | |
93 | ||
c7699875 | 94 | //fprintf(stderr, "ALGO_NAME - algorithm name\n"); |
95 | //fprintf(stderr, "ALGO_KEY - algorithm key\n"); | |
96 | ||
eaa34ee3 | 97 | fprintf(stderr, "SELECTOR := src ADDR[/PLEN] dst ADDR[/PLEN] [ UPSPEC ] [ dev DEV ]\n"); |
c7699875 | 98 | |
c70b36d2 | 99 | fprintf(stderr, "UPSPEC := proto PROTO [ [ sport PORT ] [ dport PORT ] |\n"); |
100 | fprintf(stderr, " [ type NUMBER ] [ code NUMBER ] ]\n"); | |
101 | ||
c7699875 | 102 | |
103 | //fprintf(stderr, "DEV - device name(default=none)\n"); | |
104 | fprintf(stderr, "LIMIT-LIST := [ LIMIT-LIST ] | [ limit LIMIT ]\n"); | |
105 | fprintf(stderr, "LIMIT := [ [time-soft|time-hard|time-use-soft|time-use-hard] SECONDS ] |\n"); | |
106 | fprintf(stderr, " [ [byte-soft|byte-hard] SIZE ] | [ [packet-soft|packet-hard] COUNT ]\n"); | |
107 | exit(-1); | |
108 | } | |
109 | ||
110 | static int xfrm_algo_parse(struct xfrm_algo *alg, enum xfrm_attr_type_t type, | |
111 | char *name, char *key, int max) | |
112 | { | |
113 | int len; | |
7809c616 | 114 | int slen = strlen(key); |
c7699875 | 115 | |
eaa34ee3 | 116 | #if 0 |
c7699875 | 117 | /* XXX: verifying both name and key is required! */ |
118 | fprintf(stderr, "warning: ALGONAME/ALGOKEY will send to kernel promiscuously!(verifying them isn't implemented yet)\n"); | |
119 | #endif | |
120 | ||
121 | strncpy(alg->alg_name, name, sizeof(alg->alg_name)); | |
122 | ||
7809c616 | 123 | if (slen > 2 && strncmp(key, "0x", 2) == 0) { |
54f7328a | 124 | /* split two chars "0x" from the top */ |
125 | char *p = key + 2; | |
126 | int plen = slen - 2; | |
127 | int i; | |
128 | int j; | |
129 | ||
130 | /* Converting hexadecimal numbered string into real key; | |
131 | * Convert each two chars into one char(value). If number | |
132 | * of the length is odd, add zero on the top for rounding. | |
c7699875 | 133 | */ |
7809c616 | 134 | |
54f7328a | 135 | /* calculate length of the converted values(real key) */ |
136 | len = (plen + 1) / 2; | |
137 | if (len > max) | |
138 | invarg("\"ALGOKEY\" makes buffer overflow\n", key); | |
c7699875 | 139 | |
54f7328a | 140 | for (i = - (plen % 2), j = 0; j < len; i += 2, j++) { |
141 | char vbuf[3]; | |
142 | char val; | |
c7699875 | 143 | |
54f7328a | 144 | vbuf[0] = i >= 0 ? p[i] : '0'; |
145 | vbuf[1] = p[i + 1]; | |
146 | vbuf[2] = '\0'; | |
c7699875 | 147 | |
54f7328a | 148 | if (get_u8(&val, vbuf, 16)) |
149 | invarg("\"ALGOKEY\" is invalid", key); | |
7809c616 | 150 | |
54f7328a | 151 | alg->alg_key[j] = val; |
c7699875 | 152 | } |
c7699875 | 153 | } else { |
7809c616 | 154 | len = slen; |
c7699875 | 155 | if (len > 0) { |
156 | if (len > max) | |
157 | invarg("\"ALGOKEY\" makes buffer overflow\n", key); | |
158 | ||
159 | strncpy(alg->alg_key, key, len); | |
160 | } | |
161 | } | |
162 | ||
163 | alg->alg_key_len = len * 8; | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
fb7399b2 | 168 | static int xfrm_seq_parse(__u32 *seq, int *argcp, char ***argvp) |
169 | { | |
170 | int argc = *argcp; | |
171 | char **argv = *argvp; | |
172 | ||
173 | if (get_u32(seq, *argv, 0)) | |
174 | invarg("\"SEQ\" is invalid", *argv); | |
175 | ||
176 | *seq = htonl(*seq); | |
177 | ||
178 | *argcp = argc; | |
179 | *argvp = argv; | |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
c7699875 | 184 | static int xfrm_state_flag_parse(__u8 *flags, int *argcp, char ***argvp) |
185 | { | |
186 | int argc = *argcp; | |
187 | char **argv = *argvp; | |
9e566a46 | 188 | int len = strlen(*argv); |
189 | ||
190 | if (len > 2 && strncmp(*argv, "0x", 2) == 0) { | |
191 | __u8 val = 0; | |
c7699875 | 192 | |
9e566a46 | 193 | if (get_u8(&val, *argv, 16)) |
194 | invarg("\"FLAG\" is invalid", *argv); | |
195 | *flags = val; | |
196 | } else { | |
eaa34ee3 | 197 | while (1) { |
198 | if (strcmp(*argv, "noecn") == 0) | |
199 | *flags |= XFRM_STATE_NOECN; | |
200 | else if (strcmp(*argv, "decap-dscp") == 0) | |
201 | *flags |= XFRM_STATE_DECAP_DSCP; | |
202 | else { | |
203 | PREV_ARG(); /* back track */ | |
204 | break; | |
205 | } | |
206 | ||
207 | if (!NEXT_ARG_OK()) | |
208 | break; | |
209 | NEXT_ARG(); | |
210 | } | |
9e566a46 | 211 | } |
c7699875 | 212 | |
213 | filter.state_flags_mask = XFRM_FILTER_MASK_FULL; | |
214 | ||
215 | *argcp = argc; | |
216 | *argvp = argv; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
221 | static int xfrm_state_modify(int cmd, unsigned flags, int argc, char **argv) | |
222 | { | |
223 | struct rtnl_handle rth; | |
224 | struct { | |
225 | struct nlmsghdr n; | |
226 | struct xfrm_usersa_info xsinfo; | |
227 | char buf[RTA_BUF_SIZE]; | |
228 | } req; | |
229 | char *idp = NULL; | |
230 | char *ealgop = NULL; | |
231 | char *aalgop = NULL; | |
232 | char *calgop = NULL; | |
233 | ||
234 | memset(&req, 0, sizeof(req)); | |
235 | ||
236 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsinfo)); | |
237 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; | |
238 | req.n.nlmsg_type = cmd; | |
239 | req.xsinfo.family = preferred_family; | |
240 | ||
241 | req.xsinfo.lft.soft_byte_limit = XFRM_INF; | |
242 | req.xsinfo.lft.hard_byte_limit = XFRM_INF; | |
243 | req.xsinfo.lft.soft_packet_limit = XFRM_INF; | |
244 | req.xsinfo.lft.hard_packet_limit = XFRM_INF; | |
245 | ||
246 | while (argc > 0) { | |
7809c616 | 247 | if (strcmp(*argv, "mode") == 0) { |
c7699875 | 248 | NEXT_ARG(); |
249 | xfrm_mode_parse(&req.xsinfo.mode, &argc, &argv); | |
250 | } else if (strcmp(*argv, "reqid") == 0) { | |
251 | NEXT_ARG(); | |
252 | xfrm_reqid_parse(&req.xsinfo.reqid, &argc, &argv); | |
fb7399b2 | 253 | } else if (strcmp(*argv, "seq") == 0) { |
254 | NEXT_ARG(); | |
255 | xfrm_seq_parse(&req.xsinfo.seq, &argc, &argv); | |
eaa34ee3 | 256 | } else if (strcmp(*argv, "replay-window") == 0) { |
257 | NEXT_ARG(); | |
258 | if (get_u8(&req.xsinfo.replay_window, *argv, 0)) | |
259 | invarg("\"replay-window\" value is invalid", *argv); | |
c7699875 | 260 | } else if (strcmp(*argv, "flag") == 0) { |
261 | NEXT_ARG(); | |
262 | xfrm_state_flag_parse(&req.xsinfo.flags, &argc, &argv); | |
263 | } else if (strcmp(*argv, "sel") == 0) { | |
264 | NEXT_ARG(); | |
265 | xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv); | |
c7699875 | 266 | } else if (strcmp(*argv, "limit") == 0) { |
267 | NEXT_ARG(); | |
268 | xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv); | |
5cf576d9 SH |
269 | } else if (strcmp(*argv, "encap") == 0) { |
270 | struct xfrm_encap_tmpl encap; | |
271 | inet_prefix oa; | |
272 | NEXT_ARG(); | |
273 | xfrm_encap_type_parse(&encap.encap_type, &argc, &argv); | |
274 | NEXT_ARG(); | |
275 | if (get_u16(&encap.encap_sport, *argv, 0)) | |
276 | invarg("\"encap\" sport value is invalid", *argv); | |
277 | encap.encap_sport = htons(encap.encap_sport); | |
278 | NEXT_ARG(); | |
279 | if (get_u16(&encap.encap_dport, *argv, 0)) | |
280 | invarg("\"encap\" dport value is invalid", *argv); | |
281 | encap.encap_dport = htons(encap.encap_dport); | |
282 | NEXT_ARG(); | |
283 | get_addr(&oa, *argv, AF_UNSPEC); | |
284 | memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa)); | |
285 | addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP, | |
286 | (void *)&encap, sizeof(encap)); | |
c7699875 | 287 | } else { |
7809c616 | 288 | /* try to assume ALGO */ |
289 | int type = xfrm_algotype_getbyname(*argv); | |
290 | switch (type) { | |
291 | case XFRMA_ALG_CRYPT: | |
292 | case XFRMA_ALG_AUTH: | |
293 | case XFRMA_ALG_COMP: | |
294 | { | |
295 | /* ALGO */ | |
296 | struct { | |
297 | struct xfrm_algo alg; | |
298 | char buf[XFRM_ALGO_KEY_BUF_SIZE]; | |
299 | } alg; | |
300 | int len; | |
301 | char *name; | |
302 | char *key; | |
303 | ||
304 | switch (type) { | |
305 | case XFRMA_ALG_CRYPT: | |
306 | if (ealgop) | |
307 | duparg("ALGOTYPE", *argv); | |
308 | ealgop = *argv; | |
309 | break; | |
310 | case XFRMA_ALG_AUTH: | |
311 | if (aalgop) | |
312 | duparg("ALGOTYPE", *argv); | |
313 | aalgop = *argv; | |
314 | break; | |
315 | case XFRMA_ALG_COMP: | |
316 | if (calgop) | |
317 | duparg("ALGOTYPE", *argv); | |
318 | calgop = *argv; | |
319 | break; | |
320 | default: | |
321 | /* not reached */ | |
322 | invarg("\"ALGOTYPE\" is invalid\n", *argv); | |
323 | } | |
324 | ||
325 | if (!NEXT_ARG_OK()) | |
326 | missarg("ALGONAME"); | |
327 | NEXT_ARG(); | |
328 | name = *argv; | |
329 | ||
330 | if (!NEXT_ARG_OK()) | |
331 | missarg("ALGOKEY"); | |
332 | NEXT_ARG(); | |
333 | key = *argv; | |
334 | ||
335 | memset(&alg, 0, sizeof(alg)); | |
336 | ||
337 | xfrm_algo_parse((void *)&alg, type, name, key, | |
338 | sizeof(alg.buf)); | |
339 | len = sizeof(struct xfrm_algo) + alg.alg.alg_key_len; | |
340 | ||
341 | addattr_l(&req.n, sizeof(req.buf), type, | |
342 | (void *)&alg, len); | |
343 | break; | |
344 | } | |
345 | default: | |
346 | /* try to assume ID */ | |
347 | if (idp) | |
348 | invarg("unknown", *argv); | |
349 | idp = *argv; | |
350 | ||
351 | /* ID */ | |
352 | xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id, | |
353 | &req.xsinfo.family, 0, &argc, &argv); | |
354 | if (preferred_family == AF_UNSPEC) | |
355 | preferred_family = req.xsinfo.family; | |
356 | } | |
c7699875 | 357 | } |
358 | argc--; argv++; | |
359 | } | |
360 | ||
361 | if (!idp) { | |
362 | fprintf(stderr, "Not enough information: \"ID\" is required\n"); | |
363 | exit(1); | |
364 | } | |
365 | ||
366 | if (ealgop || aalgop || calgop) { | |
367 | if (req.xsinfo.id.proto != IPPROTO_ESP && | |
368 | req.xsinfo.id.proto != IPPROTO_AH && | |
369 | req.xsinfo.id.proto != IPPROTO_COMP) { | |
29aa4dd7 | 370 | fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n", strxf_xfrmproto(req.xsinfo.id.proto)); |
c7699875 | 371 | exit(1); |
372 | } | |
373 | } else { | |
374 | if (req.xsinfo.id.proto == IPPROTO_ESP || | |
375 | req.xsinfo.id.proto == IPPROTO_AH || | |
376 | req.xsinfo.id.proto == IPPROTO_COMP) { | |
29aa4dd7 | 377 | fprintf(stderr, "\"ALGO\" is required with proto=%s\n", strxf_xfrmproto(req.xsinfo.id.proto)); |
c7699875 | 378 | exit (1); |
379 | } | |
380 | } | |
381 | ||
382 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
383 | exit(1); | |
384 | ||
385 | if (req.xsinfo.family == AF_UNSPEC) | |
386 | req.xsinfo.family = AF_INET; | |
387 | ||
388 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
389 | exit(2); | |
390 | ||
391 | rtnl_close(&rth); | |
392 | ||
393 | return 0; | |
394 | } | |
395 | ||
fb7399b2 | 396 | static int xfrm_state_allocspi(int argc, char **argv) |
397 | { | |
398 | struct rtnl_handle rth; | |
399 | struct { | |
400 | struct nlmsghdr n; | |
401 | struct xfrm_userspi_info xspi; | |
402 | char buf[RTA_BUF_SIZE]; | |
403 | } req; | |
404 | char *idp = NULL; | |
405 | char *minp = NULL; | |
406 | char *maxp = NULL; | |
407 | char res_buf[NLMSG_BUF_SIZE]; | |
408 | struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf; | |
409 | ||
410 | memset(res_buf, 0, sizeof(res_buf)); | |
411 | ||
412 | memset(&req, 0, sizeof(req)); | |
413 | ||
414 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi)); | |
415 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
416 | req.n.nlmsg_type = XFRM_MSG_ALLOCSPI; | |
417 | req.xspi.info.family = preferred_family; | |
418 | ||
419 | #if 0 | |
420 | req.xsinfo.lft.soft_byte_limit = XFRM_INF; | |
421 | req.xsinfo.lft.hard_byte_limit = XFRM_INF; | |
422 | req.xsinfo.lft.soft_packet_limit = XFRM_INF; | |
423 | req.xsinfo.lft.hard_packet_limit = XFRM_INF; | |
424 | #endif | |
425 | ||
426 | while (argc > 0) { | |
427 | if (strcmp(*argv, "mode") == 0) { | |
428 | NEXT_ARG(); | |
429 | xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv); | |
430 | } else if (strcmp(*argv, "reqid") == 0) { | |
431 | NEXT_ARG(); | |
432 | xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv); | |
433 | } else if (strcmp(*argv, "seq") == 0) { | |
434 | NEXT_ARG(); | |
435 | xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv); | |
436 | } else if (strcmp(*argv, "min") == 0) { | |
437 | if (minp) | |
438 | duparg("min", *argv); | |
439 | minp = *argv; | |
440 | ||
441 | NEXT_ARG(); | |
442 | ||
443 | if (get_u32(&req.xspi.min, *argv, 0)) | |
444 | invarg("\"min\" value is invalid", *argv); | |
445 | } else if (strcmp(*argv, "max") == 0) { | |
446 | if (maxp) | |
447 | duparg("max", *argv); | |
448 | maxp = *argv; | |
449 | ||
450 | NEXT_ARG(); | |
451 | ||
452 | if (get_u32(&req.xspi.max, *argv, 0)) | |
453 | invarg("\"max\" value is invalid", *argv); | |
454 | } else { | |
455 | /* try to assume ID */ | |
456 | if (idp) | |
457 | invarg("unknown", *argv); | |
458 | idp = *argv; | |
459 | ||
460 | /* ID */ | |
461 | xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id, | |
462 | &req.xspi.info.family, 0, &argc, &argv); | |
463 | if (req.xspi.info.id.spi) { | |
464 | fprintf(stderr, "\"SPI\" must be zero\n"); | |
465 | exit(1); | |
466 | } | |
467 | if (preferred_family == AF_UNSPEC) | |
468 | preferred_family = req.xspi.info.family; | |
469 | } | |
470 | argc--; argv++; | |
471 | } | |
472 | ||
473 | if (!idp) { | |
474 | fprintf(stderr, "Not enough information: \"ID\" is required\n"); | |
475 | exit(1); | |
476 | } | |
477 | ||
478 | if (minp) { | |
479 | if (!maxp) { | |
480 | fprintf(stderr, "\"max\" is missing\n"); | |
481 | exit(1); | |
482 | } | |
483 | if (req.xspi.min > req.xspi.max) { | |
484 | fprintf(stderr, "\"min\" valie is larger than \"max\" one\n"); | |
485 | exit(1); | |
486 | } | |
487 | } else { | |
488 | if (maxp) { | |
489 | fprintf(stderr, "\"min\" is missing\n"); | |
490 | exit(1); | |
491 | } | |
492 | ||
493 | /* XXX: Default value defined in PF_KEY; | |
494 | * See kernel's net/key/af_key.c(pfkey_getspi). | |
495 | */ | |
496 | req.xspi.min = 0x100; | |
497 | req.xspi.max = 0x0fffffff; | |
498 | ||
499 | /* XXX: IPCOMP spi is 16-bits; | |
500 | * See kernel's net/xfrm/xfrm_user(verify_userspi_info). | |
501 | */ | |
502 | if (req.xspi.info.id.proto == IPPROTO_COMP) | |
503 | req.xspi.max = 0xffff; | |
504 | } | |
505 | ||
506 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
507 | exit(1); | |
508 | ||
509 | if (req.xspi.info.family == AF_UNSPEC) | |
510 | req.xspi.info.family = AF_INET; | |
511 | ||
512 | ||
513 | if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0) | |
514 | exit(2); | |
515 | ||
516 | if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) { | |
517 | fprintf(stderr, "An error :-)\n"); | |
518 | exit(1); | |
519 | } | |
520 | ||
521 | rtnl_close(&rth); | |
522 | ||
523 | return 0; | |
524 | } | |
525 | ||
c7699875 | 526 | static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo) |
527 | { | |
528 | if (!filter.use) | |
529 | return 1; | |
530 | ||
531 | if (filter.id_src_mask) | |
eaa34ee3 | 532 | if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr, |
533 | filter.id_src_mask)) | |
c7699875 | 534 | return 0; |
535 | if (filter.id_dst_mask) | |
eaa34ee3 | 536 | if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr, |
537 | filter.id_dst_mask)) | |
c7699875 | 538 | return 0; |
539 | if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask) | |
540 | return 0; | |
541 | if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask) | |
542 | return 0; | |
543 | if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask) | |
544 | return 0; | |
545 | if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask) | |
546 | return 0; | |
547 | if (filter.state_flags_mask) | |
548 | if ((xsinfo->flags & filter.xsinfo.flags) == 0) | |
549 | return 0; | |
550 | ||
551 | return 1; | |
552 | } | |
553 | ||
fb7399b2 | 554 | int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n, |
555 | void *arg) | |
c7699875 | 556 | { |
557 | FILE *fp = (FILE*)arg; | |
558 | struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n); | |
559 | int len = n->nlmsg_len; | |
560 | struct rtattr * tb[XFRMA_MAX+1]; | |
c7699875 | 561 | |
562 | if (n->nlmsg_type != XFRM_MSG_NEWSA && | |
563 | n->nlmsg_type != XFRM_MSG_DELSA) { | |
564 | fprintf(stderr, "Not a state: %08x %08x %08x\n", | |
565 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
566 | return 0; | |
567 | } | |
568 | ||
569 | len -= NLMSG_LENGTH(sizeof(*xsinfo)); | |
570 | if (len < 0) { | |
571 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
572 | return -1; | |
573 | } | |
574 | ||
575 | if (!xfrm_state_filter_match(xsinfo)) | |
576 | return 0; | |
577 | ||
2534613e | 578 | parse_rtattr(tb, XFRMA_MAX, XFRMS_RTA(xsinfo), len); |
c7699875 | 579 | |
580 | if (n->nlmsg_type == XFRM_MSG_DELSA) | |
581 | fprintf(fp, "Deleted "); | |
582 | ||
fb7399b2 | 583 | xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL); |
c7699875 | 584 | |
7809c616 | 585 | if (oneline) |
586 | fprintf(fp, "\n"); | |
587 | ||
c7699875 | 588 | return 0; |
589 | } | |
590 | ||
591 | static int xfrm_state_get_or_delete(int argc, char **argv, int delete) | |
592 | { | |
593 | struct rtnl_handle rth; | |
594 | struct { | |
595 | struct nlmsghdr n; | |
596 | struct xfrm_usersa_id xsid; | |
597 | } req; | |
598 | struct xfrm_id id; | |
599 | char *idp = NULL; | |
600 | ||
601 | memset(&req, 0, sizeof(req)); | |
602 | ||
603 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid)); | |
604 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
605 | req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA; | |
606 | req.xsid.family = preferred_family; | |
607 | ||
608 | while (argc > 0) { | |
609 | /* | |
610 | * XXX: Source address is not used and ignore it to follow | |
611 | * XXX: a manner of setkey e.g. in the case of deleting/getting | |
612 | * XXX: message of IPsec SA. | |
613 | */ | |
614 | xfrm_address_t ignore_saddr; | |
615 | ||
616 | if (idp) | |
617 | invarg("unknown", *argv); | |
618 | idp = *argv; | |
619 | ||
620 | /* ID */ | |
621 | memset(&id, 0, sizeof(id)); | |
7809c616 | 622 | xfrm_id_parse(&ignore_saddr, &id, &req.xsid.family, 0, |
c7699875 | 623 | &argc, &argv); |
624 | ||
625 | memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr)); | |
626 | req.xsid.spi = id.spi; | |
627 | req.xsid.proto = id.proto; | |
628 | ||
629 | argc--; argv++; | |
630 | } | |
631 | ||
632 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
633 | exit(1); | |
634 | ||
635 | if (req.xsid.family == AF_UNSPEC) | |
636 | req.xsid.family = AF_INET; | |
637 | ||
638 | if (delete) { | |
639 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
640 | exit(2); | |
641 | } else { | |
642 | char buf[NLMSG_BUF_SIZE]; | |
643 | struct nlmsghdr *res_n = (struct nlmsghdr *)buf; | |
644 | ||
645 | memset(buf, 0, sizeof(buf)); | |
646 | ||
647 | if (rtnl_talk(&rth, &req.n, 0, 0, res_n, NULL, NULL) < 0) | |
648 | exit(2); | |
649 | ||
650 | if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) { | |
651 | fprintf(stderr, "An error :-)\n"); | |
652 | exit(1); | |
653 | } | |
654 | } | |
655 | ||
656 | rtnl_close(&rth); | |
657 | ||
658 | return 0; | |
659 | } | |
660 | ||
661 | /* | |
662 | * With an existing state of nlmsg, make new nlmsg for deleting the state | |
663 | * and store it to buffer. | |
664 | */ | |
6dc9f016 | 665 | static int xfrm_state_keep(const struct sockaddr_nl *who, |
50772dc5 | 666 | struct nlmsghdr *n, |
6dc9f016 | 667 | void *arg) |
c7699875 | 668 | { |
669 | struct xfrm_buffer *xb = (struct xfrm_buffer *)arg; | |
670 | struct rtnl_handle *rth = xb->rth; | |
671 | struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n); | |
672 | int len = n->nlmsg_len; | |
673 | struct nlmsghdr *new_n; | |
674 | struct xfrm_usersa_id *xsid; | |
675 | ||
676 | if (n->nlmsg_type != XFRM_MSG_NEWSA) { | |
677 | fprintf(stderr, "Not a state: %08x %08x %08x\n", | |
678 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
679 | return 0; | |
680 | } | |
681 | ||
682 | len -= NLMSG_LENGTH(sizeof(*xsinfo)); | |
683 | if (len < 0) { | |
684 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
685 | return -1; | |
686 | } | |
687 | ||
688 | if (!xfrm_state_filter_match(xsinfo)) | |
689 | return 0; | |
690 | ||
691 | if (xb->offset > xb->size) { | |
692 | fprintf(stderr, "Flush buffer overflow\n"); | |
693 | return -1; | |
694 | } | |
695 | ||
696 | new_n = (struct nlmsghdr *)(xb->buf + xb->offset); | |
697 | new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid)); | |
698 | new_n->nlmsg_flags = NLM_F_REQUEST; | |
699 | new_n->nlmsg_type = XFRM_MSG_DELSA; | |
700 | new_n->nlmsg_seq = ++rth->seq; | |
701 | ||
702 | xsid = NLMSG_DATA(new_n); | |
703 | xsid->family = xsinfo->family; | |
704 | memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr)); | |
705 | xsid->spi = xsinfo->id.spi; | |
706 | xsid->proto = xsinfo->id.proto; | |
707 | ||
708 | xb->offset += new_n->nlmsg_len; | |
709 | xb->nlmsg_count ++; | |
710 | ||
711 | return 0; | |
712 | } | |
713 | ||
714 | static int xfrm_state_list_or_flush(int argc, char **argv, int flush) | |
715 | { | |
716 | char *idp = NULL; | |
717 | struct rtnl_handle rth; | |
718 | ||
bd641cd6 | 719 | if(argc > 0) |
720 | filter.use = 1; | |
c7699875 | 721 | filter.xsinfo.family = preferred_family; |
722 | ||
723 | while (argc > 0) { | |
724 | if (strcmp(*argv, "mode") == 0) { | |
725 | NEXT_ARG(); | |
726 | xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv); | |
727 | ||
728 | filter.mode_mask = XFRM_FILTER_MASK_FULL; | |
729 | ||
730 | } else if (strcmp(*argv, "reqid") == 0) { | |
731 | NEXT_ARG(); | |
732 | xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv); | |
733 | ||
734 | filter.reqid_mask = XFRM_FILTER_MASK_FULL; | |
735 | ||
736 | } else if (strcmp(*argv, "flag") == 0) { | |
737 | NEXT_ARG(); | |
738 | xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv); | |
739 | ||
740 | filter.state_flags_mask = XFRM_FILTER_MASK_FULL; | |
741 | ||
742 | } else { | |
743 | if (idp) | |
744 | invarg("unknown", *argv); | |
745 | idp = *argv; | |
746 | ||
747 | /* ID */ | |
7809c616 | 748 | xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id, |
749 | &filter.xsinfo.family, 1, &argc, &argv); | |
c7699875 | 750 | if (preferred_family == AF_UNSPEC) |
751 | preferred_family = filter.xsinfo.family; | |
752 | } | |
753 | argc--; argv++; | |
754 | } | |
755 | ||
756 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
757 | exit(1); | |
758 | ||
759 | if (flush) { | |
760 | struct xfrm_buffer xb; | |
761 | char buf[NLMSG_FLUSH_BUF_SIZE]; | |
762 | int i; | |
763 | ||
764 | xb.buf = buf; | |
765 | xb.size = sizeof(buf); | |
766 | xb.rth = &rth; | |
767 | ||
768 | for (i = 0; ; i++) { | |
769 | xb.offset = 0; | |
770 | xb.nlmsg_count = 0; | |
771 | ||
772 | if (show_stats > 1) | |
773 | fprintf(stderr, "Flush round = %d\n", i); | |
774 | ||
775 | if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) { | |
776 | perror("Cannot send dump request"); | |
777 | exit(1); | |
778 | } | |
779 | ||
780 | if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb, NULL, NULL) < 0) { | |
781 | fprintf(stderr, "Flush terminated\n"); | |
782 | exit(1); | |
783 | } | |
784 | if (xb.nlmsg_count == 0) { | |
785 | if (show_stats > 1) | |
786 | fprintf(stderr, "Flush completed\n"); | |
787 | break; | |
788 | } | |
789 | ||
790 | if (rtnl_send(&rth, xb.buf, xb.offset) < 0) { | |
791 | perror("Failed to send flush request\n"); | |
792 | exit(1); | |
793 | } | |
794 | if (show_stats > 1) | |
795 | fprintf(stderr, "Flushed nlmsg count = %d\n", xb.nlmsg_count); | |
796 | ||
797 | xb.offset = 0; | |
798 | xb.nlmsg_count = 0; | |
799 | } | |
800 | ||
801 | } else { | |
802 | if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) { | |
803 | perror("Cannot send dump request"); | |
804 | exit(1); | |
805 | } | |
806 | ||
807 | if (rtnl_dump_filter(&rth, xfrm_state_print, stdout, NULL, NULL) < 0) { | |
808 | fprintf(stderr, "Dump terminated\n"); | |
809 | exit(1); | |
810 | } | |
811 | } | |
812 | ||
813 | rtnl_close(&rth); | |
814 | ||
815 | exit(0); | |
816 | } | |
817 | ||
bd641cd6 | 818 | static int xfrm_state_flush_all(void) |
819 | { | |
820 | struct rtnl_handle rth; | |
821 | struct { | |
822 | struct nlmsghdr n; | |
823 | struct xfrm_usersa_flush xsf; | |
824 | } req; | |
825 | ||
826 | memset(&req, 0, sizeof(req)); | |
827 | ||
828 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf)); | |
829 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
830 | req.n.nlmsg_type = XFRM_MSG_FLUSHSA; | |
831 | req.xsf.proto = IPSEC_PROTO_ANY; | |
832 | ||
833 | if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0) | |
834 | exit(1); | |
835 | ||
836 | if (show_stats > 1) | |
837 | fprintf(stderr, "Flush all\n"); | |
838 | ||
839 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
840 | exit(2); | |
841 | ||
842 | rtnl_close(&rth); | |
843 | ||
844 | return 0; | |
845 | } | |
846 | ||
c7699875 | 847 | int do_xfrm_state(int argc, char **argv) |
848 | { | |
849 | if (argc < 1) | |
850 | return xfrm_state_list_or_flush(0, NULL, 0); | |
851 | ||
852 | if (matches(*argv, "add") == 0) | |
853 | return xfrm_state_modify(XFRM_MSG_NEWSA, 0, | |
854 | argc-1, argv+1); | |
855 | if (matches(*argv, "update") == 0) | |
856 | return xfrm_state_modify(XFRM_MSG_UPDSA, 0, | |
857 | argc-1, argv+1); | |
fb7399b2 | 858 | if (matches(*argv, "allocspi") == 0) |
859 | return xfrm_state_allocspi(argc-1, argv+1); | |
c7699875 | 860 | if (matches(*argv, "delete") == 0 || matches(*argv, "del") == 0) |
861 | return xfrm_state_get_or_delete(argc-1, argv+1, 1); | |
862 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 | |
863 | || matches(*argv, "lst") == 0) | |
864 | return xfrm_state_list_or_flush(argc-1, argv+1, 0); | |
865 | if (matches(*argv, "get") == 0) | |
866 | return xfrm_state_get_or_delete(argc-1, argv+1, 0); | |
bd641cd6 | 867 | if (matches(*argv, "flush") == 0) { |
868 | if (argc-1 < 1) | |
869 | return xfrm_state_flush_all(); | |
870 | else | |
871 | return xfrm_state_list_or_flush(argc-1, argv+1, 1); | |
872 | } | |
c7699875 | 873 | if (matches(*argv, "help") == 0) |
874 | usage(); | |
875 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv); | |
876 | exit(-1); | |
877 | } |