]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/xfrm_state.c
ip/xfrm: Do not allow redundant algorithm combinations to be specified
[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 (ealgop || aalgop || aeadop)
383 duparg("ALGO-TYPE", *argv);
384 aeadop = *argv;
385 break;
386 case XFRMA_ALG_CRYPT:
387 if (ealgop || aeadop)
388 duparg("ALGO-TYPE", *argv);
389 ealgop = *argv;
390 break;
391 case XFRMA_ALG_AUTH:
392 case XFRMA_ALG_AUTH_TRUNC:
393 if (aalgop || aeadop)
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 break;
506 default:
507 break;
508 }
509
510 if (aeadop || ealgop || aalgop || calgop) {
511 if (!xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
512 fprintf(stderr, "\"ALGO\" is invalid with proto=%s\n",
513 strxf_xfrmproto(req.xsinfo.id.proto));
514 exit(1);
515 }
516 } else {
517 if (xfrm_xfrmproto_is_ipsec(req.xsinfo.id.proto)) {
518 fprintf(stderr, "\"ALGO\" is required with proto=%s\n",
519 strxf_xfrmproto(req.xsinfo.id.proto));
520 exit (1);
521 }
522 }
523
524 if (coap) {
525 if (!xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
526 fprintf(stderr, "\"coa\" is invalid with proto=%s\n",
527 strxf_xfrmproto(req.xsinfo.id.proto));
528 exit(1);
529 }
530 } else {
531 if (xfrm_xfrmproto_is_ro(req.xsinfo.id.proto)) {
532 fprintf(stderr, "\"coa\" is required with proto=%s\n",
533 strxf_xfrmproto(req.xsinfo.id.proto));
534 exit (1);
535 }
536 }
537
538 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
539 exit(1);
540
541 if (req.xsinfo.family == AF_UNSPEC)
542 req.xsinfo.family = AF_INET;
543
544 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
545 exit(2);
546
547 rtnl_close(&rth);
548
549 return 0;
550 }
551
552 static int xfrm_state_allocspi(int argc, char **argv)
553 {
554 struct rtnl_handle rth;
555 struct {
556 struct nlmsghdr n;
557 struct xfrm_userspi_info xspi;
558 char buf[RTA_BUF_SIZE];
559 } req;
560 char *idp = NULL;
561 char *minp = NULL;
562 char *maxp = NULL;
563 struct xfrm_mark mark = {0, 0};
564 char res_buf[NLMSG_BUF_SIZE];
565 struct nlmsghdr *res_n = (struct nlmsghdr *)res_buf;
566
567 memset(res_buf, 0, sizeof(res_buf));
568
569 memset(&req, 0, sizeof(req));
570
571 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xspi));
572 req.n.nlmsg_flags = NLM_F_REQUEST;
573 req.n.nlmsg_type = XFRM_MSG_ALLOCSPI;
574 req.xspi.info.family = preferred_family;
575
576 #if 0
577 req.xsinfo.lft.soft_byte_limit = XFRM_INF;
578 req.xsinfo.lft.hard_byte_limit = XFRM_INF;
579 req.xsinfo.lft.soft_packet_limit = XFRM_INF;
580 req.xsinfo.lft.hard_packet_limit = XFRM_INF;
581 #endif
582
583 while (argc > 0) {
584 if (strcmp(*argv, "mode") == 0) {
585 NEXT_ARG();
586 xfrm_mode_parse(&req.xspi.info.mode, &argc, &argv);
587 } else if (strcmp(*argv, "mark") == 0) {
588 xfrm_parse_mark(&mark, &argc, &argv);
589 } else if (strcmp(*argv, "reqid") == 0) {
590 NEXT_ARG();
591 xfrm_reqid_parse(&req.xspi.info.reqid, &argc, &argv);
592 } else if (strcmp(*argv, "seq") == 0) {
593 NEXT_ARG();
594 xfrm_seq_parse(&req.xspi.info.seq, &argc, &argv);
595 } else if (strcmp(*argv, "min") == 0) {
596 if (minp)
597 duparg("min", *argv);
598 minp = *argv;
599
600 NEXT_ARG();
601
602 if (get_u32(&req.xspi.min, *argv, 0))
603 invarg("\"min\" value is invalid", *argv);
604 } else if (strcmp(*argv, "max") == 0) {
605 if (maxp)
606 duparg("max", *argv);
607 maxp = *argv;
608
609 NEXT_ARG();
610
611 if (get_u32(&req.xspi.max, *argv, 0))
612 invarg("\"max\" value is invalid", *argv);
613 } else {
614 /* try to assume ID */
615 if (idp)
616 invarg("unknown", *argv);
617 idp = *argv;
618
619 /* ID */
620 xfrm_id_parse(&req.xspi.info.saddr, &req.xspi.info.id,
621 &req.xspi.info.family, 0, &argc, &argv);
622 if (req.xspi.info.id.spi) {
623 fprintf(stderr, "\"SPI\" must be zero\n");
624 exit(1);
625 }
626 if (preferred_family == AF_UNSPEC)
627 preferred_family = req.xspi.info.family;
628 }
629 argc--; argv++;
630 }
631
632 if (!idp) {
633 fprintf(stderr, "Not enough information: \"ID\" is required\n");
634 exit(1);
635 }
636
637 if (minp) {
638 if (!maxp) {
639 fprintf(stderr, "\"max\" is missing\n");
640 exit(1);
641 }
642 if (req.xspi.min > req.xspi.max) {
643 fprintf(stderr, "\"min\" value is larger than \"max\" value\n");
644 exit(1);
645 }
646 } else {
647 if (maxp) {
648 fprintf(stderr, "\"min\" is missing\n");
649 exit(1);
650 }
651
652 /* XXX: Default value defined in PF_KEY;
653 * See kernel's net/key/af_key.c(pfkey_getspi).
654 */
655 req.xspi.min = 0x100;
656 req.xspi.max = 0x0fffffff;
657
658 /* XXX: IPCOMP spi is 16-bits;
659 * See kernel's net/xfrm/xfrm_user(verify_userspi_info).
660 */
661 if (req.xspi.info.id.proto == IPPROTO_COMP)
662 req.xspi.max = 0xffff;
663 }
664
665 if (mark.m & mark.v) {
666 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
667 (void *)&mark, sizeof(mark));
668 if (r < 0) {
669 fprintf(stderr, "XFRMA_MARK failed\n");
670 exit(1);
671 }
672 }
673
674 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
675 exit(1);
676
677 if (req.xspi.info.family == AF_UNSPEC)
678 req.xspi.info.family = AF_INET;
679
680
681 if (rtnl_talk(&rth, &req.n, 0, 0, res_n) < 0)
682 exit(2);
683
684 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
685 fprintf(stderr, "An error :-)\n");
686 exit(1);
687 }
688
689 rtnl_close(&rth);
690
691 return 0;
692 }
693
694 static int xfrm_state_filter_match(struct xfrm_usersa_info *xsinfo)
695 {
696 if (!filter.use)
697 return 1;
698
699 if (filter.id_src_mask)
700 if (xfrm_addr_match(&xsinfo->saddr, &filter.xsinfo.saddr,
701 filter.id_src_mask))
702 return 0;
703 if (filter.id_dst_mask)
704 if (xfrm_addr_match(&xsinfo->id.daddr, &filter.xsinfo.id.daddr,
705 filter.id_dst_mask))
706 return 0;
707 if ((xsinfo->id.proto^filter.xsinfo.id.proto)&filter.id_proto_mask)
708 return 0;
709 if ((xsinfo->id.spi^filter.xsinfo.id.spi)&filter.id_spi_mask)
710 return 0;
711 if ((xsinfo->mode^filter.xsinfo.mode)&filter.mode_mask)
712 return 0;
713 if ((xsinfo->reqid^filter.xsinfo.reqid)&filter.reqid_mask)
714 return 0;
715 if (filter.state_flags_mask)
716 if ((xsinfo->flags & filter.xsinfo.flags) == 0)
717 return 0;
718
719 return 1;
720 }
721
722 int xfrm_state_print(const struct sockaddr_nl *who, struct nlmsghdr *n,
723 void *arg)
724 {
725 FILE *fp = (FILE*)arg;
726 struct rtattr * tb[XFRMA_MAX+1];
727 struct rtattr * rta;
728 struct xfrm_usersa_info *xsinfo = NULL;
729 struct xfrm_user_expire *xexp = NULL;
730 struct xfrm_usersa_id *xsid = NULL;
731 int len = n->nlmsg_len;
732
733 if (n->nlmsg_type != XFRM_MSG_NEWSA &&
734 n->nlmsg_type != XFRM_MSG_DELSA &&
735 n->nlmsg_type != XFRM_MSG_UPDSA &&
736 n->nlmsg_type != XFRM_MSG_EXPIRE) {
737 fprintf(stderr, "Not a state: %08x %08x %08x\n",
738 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
739 return 0;
740 }
741
742 if (n->nlmsg_type == XFRM_MSG_DELSA) {
743 /* Dont blame me for this .. Herbert made me do it */
744 xsid = NLMSG_DATA(n);
745 len -= NLMSG_SPACE(sizeof(*xsid));
746 } else if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
747 xexp = NLMSG_DATA(n);
748 xsinfo = &xexp->state;
749 len -= NLMSG_SPACE(sizeof(*xexp));
750 } else {
751 xexp = NULL;
752 xsinfo = NLMSG_DATA(n);
753 len -= NLMSG_SPACE(sizeof(*xsinfo));
754 }
755
756 if (len < 0) {
757 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
758 return -1;
759 }
760
761 if (xsinfo && !xfrm_state_filter_match(xsinfo))
762 return 0;
763
764 if (n->nlmsg_type == XFRM_MSG_DELSA)
765 fprintf(fp, "Deleted ");
766 else if (n->nlmsg_type == XFRM_MSG_UPDSA)
767 fprintf(fp, "Updated ");
768 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
769 fprintf(fp, "Expired ");
770
771 if (n->nlmsg_type == XFRM_MSG_DELSA)
772 rta = XFRMSID_RTA(xsid);
773 else if (n->nlmsg_type == XFRM_MSG_EXPIRE)
774 rta = XFRMEXP_RTA(xexp);
775 else
776 rta = XFRMS_RTA(xsinfo);
777
778 parse_rtattr(tb, XFRMA_MAX, rta, len);
779
780 if (n->nlmsg_type == XFRM_MSG_DELSA) {
781 //xfrm_policy_id_print();
782
783 if (!tb[XFRMA_SA]) {
784 fprintf(stderr, "Buggy XFRM_MSG_DELSA: no XFRMA_SA\n");
785 return -1;
786 }
787 if (RTA_PAYLOAD(tb[XFRMA_SA]) < sizeof(*xsinfo)) {
788 fprintf(stderr, "Buggy XFRM_MSG_DELPOLICY: too short XFRMA_POLICY len\n");
789 return -1;
790 }
791 xsinfo = RTA_DATA(tb[XFRMA_SA]);
792 }
793
794 xfrm_state_info_print(xsinfo, tb, fp, NULL, NULL);
795
796 if (n->nlmsg_type == XFRM_MSG_EXPIRE) {
797 fprintf(fp, "\t");
798 fprintf(fp, "hard %u", xexp->hard);
799 fprintf(fp, "%s", _SL_);
800 }
801
802 if (oneline)
803 fprintf(fp, "\n");
804 fflush(fp);
805
806 return 0;
807 }
808
809 static int xfrm_state_get_or_delete(int argc, char **argv, int delete)
810 {
811 struct rtnl_handle rth;
812 struct {
813 struct nlmsghdr n;
814 struct xfrm_usersa_id xsid;
815 char buf[RTA_BUF_SIZE];
816 } req;
817 struct xfrm_id id;
818 char *idp = NULL;
819 struct xfrm_mark mark = {0, 0};
820
821 memset(&req, 0, sizeof(req));
822
823 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsid));
824 req.n.nlmsg_flags = NLM_F_REQUEST;
825 req.n.nlmsg_type = delete ? XFRM_MSG_DELSA : XFRM_MSG_GETSA;
826 req.xsid.family = preferred_family;
827
828 while (argc > 0) {
829 xfrm_address_t saddr;
830
831 if (strcmp(*argv, "mark") == 0) {
832 xfrm_parse_mark(&mark, &argc, &argv);
833 } else {
834 if (idp)
835 invarg("unknown", *argv);
836 idp = *argv;
837
838 /* ID */
839 memset(&id, 0, sizeof(id));
840 memset(&saddr, 0, sizeof(saddr));
841 xfrm_id_parse(&saddr, &id, &req.xsid.family, 0,
842 &argc, &argv);
843
844 memcpy(&req.xsid.daddr, &id.daddr, sizeof(req.xsid.daddr));
845 req.xsid.spi = id.spi;
846 req.xsid.proto = id.proto;
847
848 addattr_l(&req.n, sizeof(req.buf), XFRMA_SRCADDR,
849 (void *)&saddr, sizeof(saddr));
850 }
851
852 argc--; argv++;
853 }
854
855 if (mark.m & mark.v) {
856 int r = addattr_l(&req.n, sizeof(req.buf), XFRMA_MARK,
857 (void *)&mark, sizeof(mark));
858 if (r < 0) {
859 fprintf(stderr, "XFRMA_MARK failed\n");
860 exit(1);
861 }
862 }
863
864 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
865 exit(1);
866
867 if (req.xsid.family == AF_UNSPEC)
868 req.xsid.family = AF_INET;
869
870 if (delete) {
871 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
872 exit(2);
873 } else {
874 char buf[NLMSG_BUF_SIZE];
875 struct nlmsghdr *res_n = (struct nlmsghdr *)buf;
876
877 memset(buf, 0, sizeof(buf));
878
879 if (rtnl_talk(&rth, &req.n, 0, 0, res_n) < 0)
880 exit(2);
881
882 if (xfrm_state_print(NULL, res_n, (void*)stdout) < 0) {
883 fprintf(stderr, "An error :-)\n");
884 exit(1);
885 }
886 }
887
888 rtnl_close(&rth);
889
890 return 0;
891 }
892
893 /*
894 * With an existing state of nlmsg, make new nlmsg for deleting the state
895 * and store it to buffer.
896 */
897 static int xfrm_state_keep(const struct sockaddr_nl *who,
898 struct nlmsghdr *n,
899 void *arg)
900 {
901 struct xfrm_buffer *xb = (struct xfrm_buffer *)arg;
902 struct rtnl_handle *rth = xb->rth;
903 struct xfrm_usersa_info *xsinfo = NLMSG_DATA(n);
904 int len = n->nlmsg_len;
905 struct nlmsghdr *new_n;
906 struct xfrm_usersa_id *xsid;
907
908 if (n->nlmsg_type != XFRM_MSG_NEWSA) {
909 fprintf(stderr, "Not a state: %08x %08x %08x\n",
910 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
911 return 0;
912 }
913
914 len -= NLMSG_LENGTH(sizeof(*xsinfo));
915 if (len < 0) {
916 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
917 return -1;
918 }
919
920 if (!xfrm_state_filter_match(xsinfo))
921 return 0;
922
923 if (xb->offset > xb->size) {
924 fprintf(stderr, "State buffer overflow\n");
925 return -1;
926 }
927
928 new_n = (struct nlmsghdr *)(xb->buf + xb->offset);
929 new_n->nlmsg_len = NLMSG_LENGTH(sizeof(*xsid));
930 new_n->nlmsg_flags = NLM_F_REQUEST;
931 new_n->nlmsg_type = XFRM_MSG_DELSA;
932 new_n->nlmsg_seq = ++rth->seq;
933
934 xsid = NLMSG_DATA(new_n);
935 xsid->family = xsinfo->family;
936 memcpy(&xsid->daddr, &xsinfo->id.daddr, sizeof(xsid->daddr));
937 xsid->spi = xsinfo->id.spi;
938 xsid->proto = xsinfo->id.proto;
939
940 addattr_l(new_n, xb->size, XFRMA_SRCADDR, &xsinfo->saddr,
941 sizeof(xsid->daddr));
942
943 xb->offset += new_n->nlmsg_len;
944 xb->nlmsg_count ++;
945
946 return 0;
947 }
948
949 static int xfrm_state_list_or_deleteall(int argc, char **argv, int deleteall)
950 {
951 char *idp = NULL;
952 struct rtnl_handle rth;
953
954 if(argc > 0)
955 filter.use = 1;
956 filter.xsinfo.family = preferred_family;
957
958 while (argc > 0) {
959 if (strcmp(*argv, "mode") == 0) {
960 NEXT_ARG();
961 xfrm_mode_parse(&filter.xsinfo.mode, &argc, &argv);
962
963 filter.mode_mask = XFRM_FILTER_MASK_FULL;
964
965 } else if (strcmp(*argv, "reqid") == 0) {
966 NEXT_ARG();
967 xfrm_reqid_parse(&filter.xsinfo.reqid, &argc, &argv);
968
969 filter.reqid_mask = XFRM_FILTER_MASK_FULL;
970
971 } else if (strcmp(*argv, "flag") == 0) {
972 NEXT_ARG();
973 xfrm_state_flag_parse(&filter.xsinfo.flags, &argc, &argv);
974
975 filter.state_flags_mask = XFRM_FILTER_MASK_FULL;
976
977 } else {
978 if (idp)
979 invarg("unknown", *argv);
980 idp = *argv;
981
982 /* ID */
983 xfrm_id_parse(&filter.xsinfo.saddr, &filter.xsinfo.id,
984 &filter.xsinfo.family, 1, &argc, &argv);
985 if (preferred_family == AF_UNSPEC)
986 preferred_family = filter.xsinfo.family;
987 }
988 argc--; argv++;
989 }
990
991 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
992 exit(1);
993
994 if (deleteall) {
995 struct xfrm_buffer xb;
996 char buf[NLMSG_DELETEALL_BUF_SIZE];
997 int i;
998
999 xb.buf = buf;
1000 xb.size = sizeof(buf);
1001 xb.rth = &rth;
1002
1003 for (i = 0; ; i++) {
1004 xb.offset = 0;
1005 xb.nlmsg_count = 0;
1006
1007 if (show_stats > 1)
1008 fprintf(stderr, "Delete-all round = %d\n", i);
1009
1010 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
1011 perror("Cannot send dump request");
1012 exit(1);
1013 }
1014
1015 if (rtnl_dump_filter(&rth, xfrm_state_keep, &xb) < 0) {
1016 fprintf(stderr, "Delete-all terminated\n");
1017 exit(1);
1018 }
1019 if (xb.nlmsg_count == 0) {
1020 if (show_stats > 1)
1021 fprintf(stderr, "Delete-all completed\n");
1022 break;
1023 }
1024
1025 if (rtnl_send_check(&rth, xb.buf, xb.offset) < 0) {
1026 perror("Failed to send delete-all request\n");
1027 exit(1);
1028 }
1029 if (show_stats > 1)
1030 fprintf(stderr, "Delete-all nlmsg count = %d\n", xb.nlmsg_count);
1031
1032 xb.offset = 0;
1033 xb.nlmsg_count = 0;
1034 }
1035
1036 } else {
1037 if (rtnl_wilddump_request(&rth, preferred_family, XFRM_MSG_GETSA) < 0) {
1038 perror("Cannot send dump request");
1039 exit(1);
1040 }
1041
1042 if (rtnl_dump_filter(&rth, xfrm_state_print, stdout) < 0) {
1043 fprintf(stderr, "Dump terminated\n");
1044 exit(1);
1045 }
1046 }
1047
1048 rtnl_close(&rth);
1049
1050 exit(0);
1051 }
1052
1053 static int print_sadinfo(struct nlmsghdr *n, void *arg)
1054 {
1055 FILE *fp = (FILE*)arg;
1056 __u32 *f = NLMSG_DATA(n);
1057 struct rtattr *tb[XFRMA_SAD_MAX+1];
1058 struct rtattr *rta;
1059 __u32 *cnt;
1060
1061 int len = n->nlmsg_len;
1062
1063 len -= NLMSG_LENGTH(sizeof(__u32));
1064 if (len < 0) {
1065 fprintf(stderr, "SADinfo: Wrong len %d\n", len);
1066 return -1;
1067 }
1068
1069 rta = XFRMSAPD_RTA(f);
1070 parse_rtattr(tb, XFRMA_SAD_MAX, rta, len);
1071
1072 if (tb[XFRMA_SAD_CNT]) {
1073 fprintf(fp,"\t SAD");
1074 cnt = (__u32 *)RTA_DATA(tb[XFRMA_SAD_CNT]);
1075 fprintf(fp," count %d", *cnt);
1076 } else {
1077 fprintf(fp,"BAD SAD info returned\n");
1078 return -1;
1079 }
1080
1081 if (show_stats) {
1082 if (tb[XFRMA_SAD_HINFO]) {
1083 struct xfrmu_sadhinfo *si;
1084
1085 if (RTA_PAYLOAD(tb[XFRMA_SAD_HINFO]) < sizeof(*si)) {
1086 fprintf(fp,"BAD SAD length returned\n");
1087 return -1;
1088 }
1089
1090 si = RTA_DATA(tb[XFRMA_SAD_HINFO]);
1091 fprintf(fp," (buckets ");
1092 fprintf(fp,"count %d", si->sadhcnt);
1093 fprintf(fp," Max %d", si->sadhmcnt);
1094 fprintf(fp,")");
1095 }
1096 }
1097 fprintf(fp,"\n");
1098
1099 return 0;
1100 }
1101
1102 static int xfrm_sad_getinfo(int argc, char **argv)
1103 {
1104 struct rtnl_handle rth;
1105 struct {
1106 struct nlmsghdr n;
1107 __u32 flags;
1108 char ans[64];
1109 } req;
1110
1111 memset(&req, 0, sizeof(req));
1112 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.flags));
1113 req.n.nlmsg_flags = NLM_F_REQUEST;
1114 req.n.nlmsg_type = XFRM_MSG_GETSADINFO;
1115 req.flags = 0XFFFFFFFF;
1116
1117 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1118 exit(1);
1119
1120 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n) < 0)
1121 exit(2);
1122
1123 print_sadinfo(&req.n, (void*)stdout);
1124
1125 rtnl_close(&rth);
1126
1127 return 0;
1128 }
1129
1130 static int xfrm_state_flush(int argc, char **argv)
1131 {
1132 struct rtnl_handle rth;
1133 struct {
1134 struct nlmsghdr n;
1135 struct xfrm_usersa_flush xsf;
1136 } req;
1137 char *protop = NULL;
1138
1139 memset(&req, 0, sizeof(req));
1140
1141 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.xsf));
1142 req.n.nlmsg_flags = NLM_F_REQUEST;
1143 req.n.nlmsg_type = XFRM_MSG_FLUSHSA;
1144 req.xsf.proto = 0;
1145
1146 while (argc > 0) {
1147 if (strcmp(*argv, "proto") == 0) {
1148 int ret;
1149
1150 if (protop)
1151 duparg("proto", *argv);
1152 protop = *argv;
1153
1154 NEXT_ARG();
1155
1156 ret = xfrm_xfrmproto_getbyname(*argv);
1157 if (ret < 0)
1158 invarg("\"XFRM-PROTO\" is invalid", *argv);
1159
1160 req.xsf.proto = (__u8)ret;
1161 } else
1162 invarg("unknown", *argv);
1163
1164 argc--; argv++;
1165 }
1166
1167 if (rtnl_open_byproto(&rth, 0, NETLINK_XFRM) < 0)
1168 exit(1);
1169
1170 if (show_stats > 1)
1171 fprintf(stderr, "Flush state proto=%s\n",
1172 strxf_xfrmproto(req.xsf.proto));
1173
1174 if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
1175 exit(2);
1176
1177 rtnl_close(&rth);
1178
1179 return 0;
1180 }
1181
1182 int do_xfrm_state(int argc, char **argv)
1183 {
1184 if (argc < 1)
1185 return xfrm_state_list_or_deleteall(0, NULL, 0);
1186
1187 if (matches(*argv, "add") == 0)
1188 return xfrm_state_modify(XFRM_MSG_NEWSA, 0,
1189 argc-1, argv+1);
1190 if (matches(*argv, "update") == 0)
1191 return xfrm_state_modify(XFRM_MSG_UPDSA, 0,
1192 argc-1, argv+1);
1193 if (matches(*argv, "allocspi") == 0)
1194 return xfrm_state_allocspi(argc-1, argv+1);
1195 if (matches(*argv, "delete") == 0)
1196 return xfrm_state_get_or_delete(argc-1, argv+1, 1);
1197 if (matches(*argv, "deleteall") == 0 || matches(*argv, "delall") == 0)
1198 return xfrm_state_list_or_deleteall(argc-1, argv+1, 1);
1199 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
1200 || matches(*argv, "lst") == 0)
1201 return xfrm_state_list_or_deleteall(argc-1, argv+1, 0);
1202 if (matches(*argv, "get") == 0)
1203 return xfrm_state_get_or_delete(argc-1, argv+1, 0);
1204 if (matches(*argv, "flush") == 0)
1205 return xfrm_state_flush(argc-1, argv+1);
1206 if (matches(*argv, "count") == 0) {
1207 return xfrm_sad_getinfo(argc, argv);
1208 }
1209 if (matches(*argv, "help") == 0)
1210 usage();
1211 fprintf(stderr, "Command \"%s\" is unknown, try \"ip xfrm state help\".\n", *argv);
1212 exit(-1);
1213 }