]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_state.c
ip xfrm state: Allow different selector family
[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 preferred_family = AF_UNSPEC;
298 xfrm_selector_parse(&req.xsinfo.sel, &argc, &argv);
299 preferred_family = req.xsinfo.sel.family;
300 } else if (strcmp(*argv, "limit") == 0) {
301 NEXT_ARG();
302 xfrm_lifetime_cfg_parse(&req.xsinfo.lft, &argc, &argv);
303 } else if (strcmp(*argv, "encap") == 0) {
304 struct xfrm_encap_tmpl encap;
305 inet_prefix oa;
306 NEXT_ARG();
307 xfrm_encap_type_parse(&encap.encap_type, &argc, &argv);
308 NEXT_ARG();
309 if (get_u16(&encap.encap_sport, *argv, 0))
310 invarg("\"encap\" sport value is invalid", *argv);
311 encap.encap_sport = htons(encap.encap_sport);
312 NEXT_ARG();
313 if (get_u16(&encap.encap_dport, *argv, 0))
314 invarg("\"encap\" dport value is invalid", *argv);
315 encap.encap_dport = htons(encap.encap_dport);
316 NEXT_ARG();
317 get_addr(&oa, *argv, AF_UNSPEC);
318 memcpy(&encap.encap_oa, &oa.data, sizeof(encap.encap_oa));
319 addattr_l(&req.n, sizeof(req.buf), XFRMA_ENCAP,
320 (void *)&encap, sizeof(encap));
321 } else if (strcmp(*argv, "coa") == 0) {
322 inet_prefix coa;
323 xfrm_address_t xcoa;
324
325 if (coap)
326 duparg("coa", *argv);
327 coap = *argv;
328
329 NEXT_ARG();
330
331 get_prefix(&coa, *argv, preferred_family);
332 if (coa.family == AF_UNSPEC)
333 invarg("\"coa\" address family is AF_UNSPEC", *argv);
334 if (coa.bytelen > sizeof(xcoa))
335 invarg("\"coa\" address length is too large", *argv);
336
337 memset(&xcoa, 0, sizeof(xcoa));
338 memcpy(&xcoa, &coa.data, coa.bytelen);
339
340 addattr_l(&req.n, sizeof(req.buf), XFRMA_COADDR,
341 (void *)&xcoa, sizeof(xcoa));
342 } else if (strcmp(*argv, "ctx") == 0) {
343 char *context;
344
345 if (sctxp)
346 duparg("ctx", *argv);
347 sctxp = *argv;
348
349 NEXT_ARG();
350 context = *argv;
351
352 xfrm_sctx_parse((char *)&ctx.str, context, &ctx.sctx);
353 addattr_l(&req.n, sizeof(req.buf), XFRMA_SEC_CTX,
354 (void *)&ctx, ctx.sctx.len);
355 } else {
356 /* try to assume ALGO */
357 int type = xfrm_algotype_getbyname(*argv);
358 switch (type) {
359 case XFRMA_ALG_AEAD:
360 case XFRMA_ALG_CRYPT:
361 case XFRMA_ALG_AUTH:
362 case XFRMA_ALG_AUTH_TRUNC:
363 case XFRMA_ALG_COMP:
364 {
365 /* ALGO */
366 struct {
367 union {
368 struct xfrm_algo alg;
369 struct xfrm_algo_aead aead;
370 struct xfrm_algo_auth auth;
371 } u;
372 char buf[XFRM_ALGO_KEY_BUF_SIZE];
373 } alg = {};
374 int len;
375 __u32 icvlen, trunclen;
376 char *name;
377 char *key;
378 char *buf;
379
380 switch (type) {
381 case XFRMA_ALG_AEAD:
382 if (aeadop)
383 duparg("ALGO-TYPE", *argv);
384 aeadop = *argv;
385 break;
386 case XFRMA_ALG_CRYPT:
387 if (ealgop)
388 duparg("ALGO-TYPE", *argv);
389 ealgop = *argv;
390 break;
391 case XFRMA_ALG_AUTH:
392 case XFRMA_ALG_AUTH_TRUNC:
393 if (aalgop)
394 duparg("ALGO-TYPE", *argv);
395 aalgop = *argv;
396 break;
397 case XFRMA_ALG_COMP:
398 if (calgop)
399 duparg("ALGO-TYPE", *argv);
400 calgop = *argv;
401 break;
402 default:
403 /* not reached */
404 invarg("\"ALGO-TYPE\" is invalid\n", *argv);
405 }
406
407 if (!NEXT_ARG_OK())
408 missarg("ALGO-NAME");
409 NEXT_ARG();
410 name = *argv;
411
412 if (!NEXT_ARG_OK())
413 missarg("ALGO-KEY");
414 NEXT_ARG();
415 key = *argv;
416
417 buf = alg.u.alg.alg_key;
418 len = sizeof(alg.u.alg);
419
420 switch (type) {
421 case XFRMA_ALG_AEAD:
422 if (!NEXT_ARG_OK())
423 missarg("ALGO-ICV-LEN");
424 NEXT_ARG();
425 if (get_u32(&icvlen, *argv, 0))
426 invarg("\"aead\" ICV length is invalid",
427 *argv);
428 alg.u.aead.alg_icv_len = icvlen;
429
430 buf = alg.u.aead.alg_key;
431 len = sizeof(alg.u.aead);
432 break;
433 case XFRMA_ALG_AUTH_TRUNC:
434 if (!NEXT_ARG_OK())
435 missarg("ALGO-TRUNC-LEN");
436 NEXT_ARG();
437 if (get_u32(&trunclen, *argv, 0))
438 invarg("\"auth\" trunc length is invalid",
439 *argv);
440 alg.u.auth.alg_trunc_len = trunclen;
441
442 buf = alg.u.auth.alg_key;
443 len = sizeof(alg.u.auth);
444 break;
445 }
446
447 xfrm_algo_parse((void *)&alg, type, name, key,
448 buf, sizeof(alg.buf));
449 len += alg.u.alg.alg_key_len;
450
451 addattr_l(&req.n, sizeof(req.buf), type,
452 (void *)&alg, len);
453 break;
454 }
455 default:
456 /* try to assume ID */
457 if (idp)
458 invarg("unknown", *argv);
459 idp = *argv;
460
461 /* ID */
462 xfrm_id_parse(&req.xsinfo.saddr, &req.xsinfo.id,
463 &req.xsinfo.family, 0, &argc, &argv);
464 if (preferred_family == AF_UNSPEC)
465 preferred_family = req.xsinfo.family;
466 }
467 }
468 argc--; argv++;
469 }
470
471 if (replay.seq || replay.oseq)
472 addattr_l(&req.n, sizeof(req.buf), XFRMA_REPLAY_VAL,
473 (void *)&replay, sizeof(replay));
474
475 if (!idp) {
476 fprintf(stderr, "Not enough information: \"ID\" is required\n");
477 exit(1);
478 }
479
480 if (mark.m & mark.v) {
481 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
482 (void *)&mark, sizeof(mark));
483 if (r < 0) {
484 fprintf(stderr, "XFRMA_MARK failed\n");
485 exit(1);
486 }
487 }
488
489 switch (req.xsinfo.mode) {
490 case XFRM_MODE_TRANSPORT:
491 case XFRM_MODE_TUNNEL:
492 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
493 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
494 strxf_xfrmproto(req.xsinfo.id.proto));
495 exit(1);
496 }
497 break;
498 case XFRM_MODE_ROUTEOPTIMIZATION:
499 case XFRM_MODE_IN_TRIGGER:
500 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
501 fprintf(stderr, "\"mode\" is invalid with proto=%s\n",
502 strxf_xfrmproto(req.xsinfo.id.proto));
503 exit(1);
504 }
505 if (req.xsinfo.id.spi != 0) {
506 fprintf(stderr, "\"spi\" must be 0 with proto=%s\n",
507 strxf_xfrmproto(req.xsinfo.id.proto));
508 exit(1);
509 }
510 break;
511 default:
512 break;
513 }
514
515 if (aeadop || ealgop || aalgop || calgop) {
516 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
517 fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n",
518 strxf_xfrmproto(req.xsinfo.id.proto));
519 exit(1);
520 }
521 } else {
522 if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
523 fprintf(stderr, "\"ALGO\" is required with proto=%s\n",
524 strxf_xfrmproto(req.xsinfo.id.proto));
525 exit (1);
526 }
527 }
528
529 if (coap) {
530 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
531 fprintf(stderr, "\"coa\" is invalid with proto=%s\n",
532 strxf_xfrmproto(req.xsinfo.id.proto));
533 exit(1);
534 }
535 } else {
536 if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
537 fprintf(stderr, "\"coa\" is required with proto=%s\n",
538 strxf_xfrmproto(req.xsinfo.id.proto));
539 exit (1);
540 }
541 }
542
543 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
544 exit(1);
545
546 if (req.xsinfo.family == AF_UNSPEC)
547 req.xsinfo.family = AF_INET;
548
549 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
550 exit(2);
551
552 rtnl_close(&rth);
553
554 return 0;
555 }
556
557 static int xfrm_state_allocspi(int argc, char **argv)
558 {
559 struct rtnl_handle rth;
560 struct {
561 struct nlmsghdr n;
562 struct xfrm_userspi_info xspi;
563 char buf[RTA_BUF_SIZE];
564 } req;
565 char *idp = NULL;
566 char *minp = NULL;
567 char *maxp = NULL;
568 struct xfrm_mark mark = {0, 0};
569 char res_buf[NLMSG_BUF_SIZE];
570 struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf;
571
572 memset(res_buf, 0, sizeof(res_buf));
573
574 memset(&req, 0, sizeof(req));
575
576 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi));
577 req.n.nlmsg_flags = NLM_F_REQUEST;
578 req.n.nlmsg_type = XFRM_MSG_ALLOCSPI;
579 req.xspi.info.family = preferred_family;
580
581 #if 0
582 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
583 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
584 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
585 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
586 #endif
587
588 while (argc > 0) {
589 if (strcmp(*argv, "mode") == 0) {
590 NEXT_ARG();
591 xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
592 } else if (strcmp(*argv, "mark") == 0) {
593 xfrm_parse_mark(&mark, &argc, &argv);
594 } else if (strcmp(*argv, "reqid") == 0) {
595 NEXT_ARG();
596 xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv);
597 } else if (strcmp(*argv, "seq") == 0) {
598 NEXT_ARG();
599 xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv);
600 } else if (strcmp(*argv, "min") == 0) {
601 if (minp)
602 duparg("min", *argv);
603 minp = *argv;
604
605 NEXT_ARG();
606
607 if (get_u32(&req.xspi.min, *argv, 0))
608 invarg("\"min\" value is invalid", *argv);
609 } else if (strcmp(*argv, "max") == 0) {
610 if (maxp)
611 duparg("max", *argv);
612 maxp = *argv;
613
614 NEXT_ARG();
615
616 if (get_u32(&req.xspi.max, *argv, 0))
617 invarg("\"max\" value is invalid", *argv);
618 } else {
619 /* try to assume ID */
620 if (idp)
621 invarg("unknown", *argv);
622 idp = *argv;
623
624 /* ID */
625 xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id,
626 &req.xspi.info.family, 0, &argc, &argv);
627 if (req.xspi.info.id.spi) {
628 fprintf(stderr, "\"SPI\" must be zero\n");
629 exit(1);
630 }
631 if (preferred_family == AF_UNSPEC)
632 preferred_family = req.xspi.info.family;
633 }
634 argc--; argv++;
635 }
636
637 if (!idp) {
638 fprintf(stderr, "Not enough information: \"ID\" is required\n");
639 exit(1);
640 }
641
642 if (minp) {
643 if (!maxp) {
644 fprintf(stderr, "\"max\" is missing\n");
645 exit(1);
646 }
647 if (req.xspi.min > req.xspi.max) {
648 fprintf(stderr, "\"min\" value is larger than \"max\" value\n");
649 exit(1);
650 }
651 } else {
652 if (maxp) {
653 fprintf(stderr, "\"min\" is missing\n");
654 exit(1);
655 }
656
657 /* XXX: Default value defined in PF_KEY;
658 * See kernel's net/key/af_key.c(pfkey_getspi).
659 */
660 req.xspi.min = 0x100;
661 req.xspi.max = 0x0fffffff;
662
663 /* XXX: IPCOMP spi is 16-bits;
664 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
665 */
666 if (req.xspi.info.id.proto == IPPROTO_COMP)
667 req.xspi.max = 0xffff;
668 }
669
670 if (mark.m & mark.v) {
671 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
672 (void *)&mark, sizeof(mark));
673 if (r < 0) {
674 fprintf(stderr, "XFRMA_MARK failed\n");
675 exit(1);
676 }
677 }
678
679 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
680 exit(1);
681
682 if (req.xspi.info.family == AF_UNSPEC)
683 req.xspi.info.family = AF_INET;
684
685
686 if (rtnl_talk(&rth, &req.n, 0, 0, res_n) < 0)
687 exit(2);
688
689 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
690 fprintf(stderr, "An error :-)\n");
691 exit(1);
692 }
693
694 rtnl_close(&rth);
695
696 return 0;
697 }
698
699 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
700 {
701 if (!filter.use)
702 return 1;
703
704 if (filter.id_src_mask)
705 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
706 filter.id_src_mask))
707 return 0;
708 if (filter.id_dst_mask)
709 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
710 filter.id_dst_mask))
711 return 0;
712 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
713 return 0;
714 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
715 return 0;
716 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
717 return 0;
718 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
719 return 0;
720 if (filter.state_flags_mask)
721 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
722 return 0;
723
724 return 1;
725 }
726
727 int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
728 void *arg)
729 {
730 FILE *fp = (FILE*)arg;
731 struct rtattr * tb[XFRMA_MAX+1];
732 struct rtattr * rta;
733 struct xfrm_usersa_info *xsinfo = NULL;
734 struct xfrm_user_expire *xexp = NULL;
735 struct xfrm_usersa_id *xsid = NULL;
736 int len = n->nlmsg_len;
737
738 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
739 n->nlmsg_type != XFRM_MSG_DELSA &&
740 n->nlmsg_type != XFRM_MSG_UPDSA &&
741 n->nlmsg_type != XFRM_MSG_EXPIRE) {
742 fprintf(stderr, "Not a state: %08x %08x %08x\n",
743 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
744 return 0;
745 }
746
747 if (n->nlmsg_type == XFRM_MSG_DELSA) {
748 /* Dont blame me for this .. Herbert made me do it */
749 xsid = NLMSG_DATA(n);
750 len -= NLMSG_SPACE(sizeof(*xsid));
751 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
752 xexp = NLMSG_DATA(n);
753 xsinfo = &xexp->state;
754 len -= NLMSG_SPACE(sizeof(*xexp));
755 } else {
756 xexp = NULL;
757 xsinfo = NLMSG_DATA(n);
758 len -= NLMSG_SPACE(sizeof(*xsinfo));
759 }
760
761 if (len < 0) {
762 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
763 return -1;
764 }
765
766 if (xsinfo && !xfrm_state_filter_match(xsinfo))
767 return 0;
768
769 if (n->nlmsg_type == XFRM_MSG_DELSA)
770 fprintf(fp, "Deleted ");
771 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
772 fprintf(fp, "Updated ");
773 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
774 fprintf(fp, "Expired ");
775
776 if (n->nlmsg_type == XFRM_MSG_DELSA)
777 rta = XFRMSID_RTA(xsid);
778 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
779 rta = XFRMEXP_RTA(xexp);
780 else
781 rta = XFRMS_RTA(xsinfo);
782
783 parse_rtattr(tb, XFRMA_MAX, rta, len);
784
785 if (n->nlmsg_type == XFRM_MSG_DELSA) {
786 //xfrm_policy_id_print();
787
788 if (!tb[XFRMA_SA]) {
789 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
790 return -1;
791 }
792 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
793 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
794 return -1;
795 }
796 xsinfo = RTA_DATA(tb[XFRMA_SA]);
797 }
798
799 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL);
800
801 if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
802 fprintf(fp, "\t");
803 fprintf(fp, "hard %u", xexp->hard);
804 fprintf(fp, "%s", _SL_);
805 }
806
807 if (oneline)
808 fprintf(fp, "\n");
809 fflush(fp);
810
811 return 0;
812 }
813
814 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
815 {
816 struct rtnl_handle rth;
817 struct {
818 struct nlmsghdr n;
819 struct xfrm_usersa_id xsid;
820 char buf[RTA_BUF_SIZE];
821 } req;
822 struct xfrm_id id;
823 char *idp = NULL;
824 struct xfrm_mark mark = {0, 0};
825
826 memset(&req, 0, sizeof(req));
827
828 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
829 req.n.nlmsg_flags = NLM_F_REQUEST;
830 req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
831 req.xsid.family = preferred_family;
832
833 while (argc > 0) {
834 xfrm_address_t saddr;
835
836 if (strcmp(*argv, "mark") == 0) {
837 xfrm_parse_mark(&mark, &argc, &argv);
838 } else {
839 if (idp)
840 invarg("unknown", *argv);
841 idp = *argv;
842
843 /* ID */
844 memset(&id, 0, sizeof(id));
845 memset(&saddr, 0, sizeof(saddr));
846 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
847 &argc, &argv);
848
849 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
850 req.xsid.spi = id.spi;
851 req.xsid.proto = id.proto;
852
853 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
854 (void *)&saddr, sizeof(saddr));
855 }
856
857 argc--; argv++;
858 }
859
860 if (mark.m & mark.v) {
861 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
862 (void *)&mark, sizeof(mark));
863 if (r < 0) {
864 fprintf(stderr, "XFRMA_MARK failed\n");
865 exit(1);
866 }
867 }
868
869 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
870 exit(1);
871
872 if (req.xsid.family == AF_UNSPEC)
873 req.xsid.family = AF_INET;
874
875 if (delete) {
876 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
877 exit(2);
878 } else {
879 char buf[NLMSG_BUF_SIZE];
880 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
881
882 memset(buf, 0, sizeof(buf));
883
884 if (rtnl_talk(&rth, &req.n, 0, 0, res_n) < 0)
885 exit(2);
886
887 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
888 fprintf(stderr, "An error :-)\n");
889 exit(1);
890 }
891 }
892
893 rtnl_close(&rth);
894
895 return 0;
896 }
897
898 /*
899 * With an existing state of nlmsg, make new nlmsg for deleting the state
900 * and store it to buffer.
901 */
902 static int xfrm_state_keep(const struct sockaddr_nl *who,
903 struct nlmsghdr *n,
904 void *arg)
905 {
906 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
907 struct rtnl_handle *rth = xb->rth;
908 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
909 int len = n->nlmsg_len;
910 struct nlmsghdr *new_n;
911 struct xfrm_usersa_id *xsid;
912
913 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
914 fprintf(stderr, "Not a state: %08x %08x %08x\n",
915 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
916 return 0;
917 }
918
919 len -= NLMSG_LENGTH(sizeof(*xsinfo));
920 if (len < 0) {
921 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
922 return -1;
923 }
924
925 if (!xfrm_state_filter_match(xsinfo))
926 return 0;
927
928 if (xb->offset > xb->size) {
929 fprintf(stderr, "State buffer overflow\n");
930 return -1;
931 }
932
933 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
934 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
935 new_n->nlmsg_flags = NLM_F_REQUEST;
936 new_n->nlmsg_type = XFRM_MSG_DELSA;
937 new_n->nlmsg_seq = ++rth->seq;
938
939 xsid = NLMSG_DATA(new_n);
940 xsid->family = xsinfo->family;
941 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
942 xsid->spi = xsinfo->id.spi;
943 xsid->proto = xsinfo->id.proto;
944
945 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
946 sizeof(xsid->daddr));
947
948 xb->offset += new_n->nlmsg_len;
949 xb->nlmsg_count ++;
950
951 return 0;
952 }
953
954 static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
955 {
956 char *idp = NULL;
957 struct rtnl_handle rth;
958
959 if(argc > 0)
960 filter.use = 1;
961 filter.xsinfo.family = preferred_family;
962
963 while (argc > 0) {
964 if (strcmp(*argv, "mode") == 0) {
965 NEXT_ARG();
966 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
967
968 filter.mode_mask = XFRM_FILTER_MASK_FULL;
969
970 } else if (strcmp(*argv, "reqid") == 0) {
971 NEXT_ARG();
972 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
973
974 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
975
976 } else if (strcmp(*argv, "flag") == 0) {
977 NEXT_ARG();
978 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
979
980 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
981
982 } else {
983 if (idp)
984 invarg("unknown", *argv);
985 idp = *argv;
986
987 /* ID */
988 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
989 &filter.xsinfo.family, 1, &argc, &argv);
990 if (preferred_family == AF_UNSPEC)
991 preferred_family = filter.xsinfo.family;
992 }
993 argc--; argv++;
994 }
995
996 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
997 exit(1);
998
999 if (deleteall) {
1000 struct xfrm_buffer xb;
1001 char buf[NLMSG_DELETEALL_BUF_SIZE];
1002 int i;
1003
1004 xb.buf = buf;
1005 xb.size = sizeof(buf);
1006 xb.rth = &rth;
1007
1008 for (i = 0; ; i++) {
1009 xb.offset = 0;
1010 xb.nlmsg_count = 0;
1011
1012 if (show_stats > 1)
1013 fprintf(stderr, "Delete-all round = %d\n", i);
1014
1015 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
1016 perror("Cannot send dump request");
1017 exit(1);
1018 }
1019
1020 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb) < 0) {
1021 fprintf(stderr, "Delete-all terminated\n");
1022 exit(1);
1023 }
1024 if (xb.nlmsg_count == 0) {
1025 if (show_stats > 1)
1026 fprintf(stderr, "Delete-all completed\n");
1027 break;
1028 }
1029
1030 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
1031 perror("Failed to send delete-all request\n");
1032 exit(1);
1033 }
1034 if (show_stats > 1)
1035 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
1036
1037 xb.offset = 0;
1038 xb.nlmsg_count = 0;
1039 }
1040
1041 } else {
1042 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
1043 perror("Cannot send dump request");
1044 exit(1);
1045 }
1046
1047 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout) < 0) {
1048 fprintf(stderr, "Dump terminated\n");
1049 exit(1);
1050 }
1051 }
1052
1053 rtnl_close(&rth);
1054
1055 exit(0);
1056 }
1057
1058 static int print_sadinfo(struct nlmsghdr *n, void *arg)
1059 {
1060 FILE *fp = (FILE*)arg;
1061 __u32 *f = NLMSG_DATA(n);
1062 struct rtattr *tb[XFRMA_SAD_MAX+1];
1063 struct rtattr *rta;
1064 __u32 *cnt;
1065
1066 int len = n->nlmsg_len;
1067
1068 len -= NLMSG_LENGTH(sizeof(__u32));
1069 if (len < 0) {
1070 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
1071 return -1;
1072 }
1073
1074 rta = XFRMSAPD_RTA(f);
1075 parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
1076
1077 if (tb[XFRMA_SAD_CNT]) {
1078 fprintf(fp,"\t SAD");
1079 cnt = (__u32 *)RTA_DATA(tb[XFRMA_SAD_CNT]);
1080 fprintf(fp," count %d", *cnt);
1081 } else {
1082 fprintf(fp,"BAD SAD info returned\n");
1083 return -1;
1084 }
1085
1086 if (show_stats) {
1087 if (tb[XFRMA_SAD_HINFO]) {
1088 struct xfrmu_sadhinfo *si;
1089
1090 if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) {
1091 fprintf(fp,"BAD SAD length returned\n");
1092 return -1;
1093 }
1094
1095 si = RTA_DATA(tb[XFRMA_SAD_HINFO]);
1096 fprintf(fp," (buckets ");
1097 fprintf(fp,"count %d", si->sadhcnt);
1098 fprintf(fp," Max %d", si->sadhmcnt);
1099 fprintf(fp,")");
1100 }
1101 }
1102 fprintf(fp,"\n");
1103
1104 return 0;
1105 }
1106
1107 static int xfrm_sad_getinfo(int argc, char **argv)
1108 {
1109 struct rtnl_handle rth;
1110 struct {
1111 struct nlmsghdr n;
1112 __u32 flags;
1113 char ans[64];
1114 } req;
1115
1116 memset(&req, 0, sizeof(req));
1117 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags));
1118 req.n.nlmsg_flags = NLM_F_REQUEST;
1119 req.n.nlmsg_type = XFRM_MSG_GETSADINFO;
1120 req.flags = 0XFFFFFFFF;
1121
1122 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1123 exit(1);
1124
1125 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0)
1126 exit(2);
1127
1128 print_sadinfo(&req.n, (void*)stdout);
1129
1130 rtnl_close(&rth);
1131
1132 return 0;
1133 }
1134
1135 static int xfrm_state_flush(int argc, char **argv)
1136 {
1137 struct rtnl_handle rth;
1138 struct {
1139 struct nlmsghdr n;
1140 struct xfrm_usersa_flush xsf;
1141 } req;
1142 char *protop = NULL;
1143
1144 memset(&req, 0, sizeof(req));
1145
1146 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
1147 req.n.nlmsg_flags = NLM_F_REQUEST;
1148 req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
1149 req.xsf.proto = 0;
1150
1151 while (argc > 0) {
1152 if (strcmp(*argv, "proto") == 0) {
1153 int ret;
1154
1155 if (protop)
1156 duparg("proto", *argv);
1157 protop = *argv;
1158
1159 NEXT_ARG();
1160
1161 ret = xfrm_xfrmproto_getbyname(*argv);
1162 if (ret < 0)
1163 invarg("\"XFRM-PROTO\" is invalid", *argv);
1164
1165 req.xsf.proto = (__u8)ret;
1166 } else
1167 invarg("unknown", *argv);
1168
1169 argc--; argv++;
1170 }
1171
1172 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1173 exit(1);
1174
1175 if (show_stats > 1)
1176 fprintf(stderr, "Flush state proto=%s\n",
1177 strxf_xfrmproto(req.xsf.proto));
1178
1179 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
1180 exit(2);
1181
1182 rtnl_close(&rth);
1183
1184 return 0;
1185 }
1186
1187 int do_xfrm_state(int argc, char **argv)
1188 {
1189 if (argc < 1)
1190 return xfrm_state_list_or_deleteall(0, NULL, 0);
1191
1192 if (matches(*argv, "add") == 0)
1193 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1194 argc-1, argv+1);
1195 if (matches(*argv, "update") == 0)
1196 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1197 argc-1, argv+1);
1198 if (matches(*argv, "allocspi") == 0)
1199 return xfrm_state_allocspi(argc-1, argv+1);
1200 if (matches(*argv, "delete") == 0)
1201 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
1202 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1203 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
1204 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1205 || matches(*argv, "lst") == 0)
1206 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
1207 if (matches(*argv, "get") == 0)
1208 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
1209 if (matches(*argv, "flush") == 0)
1210 return xfrm_state_flush(argc-1, argv+1);
1211 if (matches(*argv, "count") == 0) {
1212 return xfrm_sad_getinfo(argc, argv);
1213 }
1214 if (matches(*argv, "help") == 0)
1215 usage();
1216 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);
1217 exit(-1);
1218 }