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